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();
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'));
}
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');
});
}
select sum(number), country_id from table group by country_id;
select sum(number), city_name, country_id from table group by country_id;
public function project(): HasOne
{
$instance = $this->newRelatedInstance(Project::class);
$relation = new HasOne($instance->newQuery(), $this, DB::raw('project_user.user_id'), $this->getKeyName());
return $relation->leftJoin('project_user', 'project_user.project_id', '=', 'projects.id');
}
Model::insert([
[row1], [row2], [row3], [row4],
]);
public static function fromRequest(FormRequest $request): static
{
$data = $request->validated();
$properties = (new \ReflectionClass(self::class))->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property){
if(!isset($data[$property->name])){
$data[$property->name] = $property->getDefaultValue();
}
}
return new static(...$data);
}
public function __invoke($attribute, $value, $fail)
{
$dishesRequest = collect($value);
$data = Dishe::whereIn('id', $dishesRequest)->Active()->whereHas('menu', function ($query) {
return $query->where('project_id', $this->project->id)->Active();
})->selectRaw('count(1) as count, sum(price) as sum');
if($data->count != $dishesRequest->count()){
return $fail(__('validation.exists'));
}
# Получаем список блюд по массиву идентификаторов
$totalDishes = $data->sum;
$total = $this->order ? $totalDishes + $this->order->totalPrice : $totalDishes;
if ($total < 1 || $total > 150000) {
return $fail('Сумма заказа должна быть от 1 ₽ до 150 000 ₽');
}
}
php artisan make:policy OrderPolicy
class OrderPolicy
{
use HandlesAuthorization;
public function update(User $user, Order $order)
{
if (!$user->isEmployee()) {
return false;
}
if ($user->id !== $order->user_id) { // остальной кусок ифа не понял - но он должен быть здесь.
return false;
}
return in_array($order->status, [Order::NEW, Order::WORK]);
}
}
class OrderUpdateRequest extends FormRequest
{
public function authorize()
{
return $this->user()->can('update', $this->order);
}
}