public function scopeOfLocale(Builder $builder, $locale = 'en'){
return $builder->where('code', '=', $locale);
}
public function handle($request, Closure $next)
{
Locale::addGlobalScope('locale', function (Builder $builder){
$builder->ofLocale(app()->getLocale());
});
}
public function locales(){
return $this->hasMany(Locale::class);
}
public function current_locale(){
return $this->hasOne(Locale::class)->ofLocale(app()->getLocale());
}
$categories = Category::with('current_locale')->get();
$categories = Category::with('locales')->get();
SELECT p.txt, count(distinct ip.ip)
FROM users u
LEFT JOIN usersip ip
ON u.id = ip.idUsers
LEFT JOIN podrazd p
ON u.podrazd = p.id
group by p.txt
order by p.txt;
SELECT p.txt, ip.ip
FROM users u
LEFT JOIN usersip ip
ON u.id = ip.idUsers
LEFT JOIN podrazd p
ON u.podrazd = p.id
group by p.txt, ip.ip
order by p.txt;
select
keys.*
from keys.*
join
(select max(issued_at) as issued_at, user_login from keys group by user_login) as last
on
last.user_login = keys.user_login and last.issued_at = keys.issued_at
select
"user_login",
(select key "user_login" from keys as last where keys.id = last.id order by issued_at desc limit 1) as key
from keys
group by "user_login"
SELECT
user_login,
LAST_VALUE(key) OVER(ORDER BY issued_at desc) as last_key
FROM
keys
group by "user_login";
SELECT
user_login,
ARRAY_AGG(key ORDER BY issued_at desc)[0]
FROM
keys
group by "user_login";
public function handle($request, Closure $next)
{
return $this->encrypt($next($this->decrypt($request)));
}
RouteServiceProvider.php
Route::bind('test', function (string $value) {
return new Test($value);
});
web.php
Route::get('/{test}', [TestController::class, 'index']);
TestController.php
class TestController extends Controller
{
public function index(Request $request, Test $test){
dd($test);
}
public function category(Request $request, $code) {
//Получаю все товары данной катигории следующим образом: $category->products
$category = Category::where('code', $code)->firstOrFail();
//А пагинация строится в отдельном запросе к таблице Товаров
$products = $category->products()
->when(($request->get('price_min'), function(Builder $query, $price_min){
return $query->where('price', '>=', $price_min);
})->when(($request->get('price_max'), function(Builder $query, $price_max){
return $query->where('price', '<=', $price_max);
})->paginate(4);
return view('category', compact('category', 'products'));
}
WITH
some_data AS ( select val from unnest(ARRAY['AAAABB', 'AAABBB']) as val)
SELECT *
FROM some_data;
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->integer('price');
$table->text('des');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories');
});
}