F
F
fgjcirifb2019-02-21 18:50:48
Laravel
fgjcirifb, 2019-02-21 18:50:48

How not to repeat queries in the database?

I validate the input using special Laravel classes that are created using make:request.
Let's say we have a class like this:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BlaBla extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $comment = Comment::find($this->route('comment'));

        return $comment && $this->user()->can('update', $comment);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [

        ];
    }
}

As you can see, in the authorize() method, we make a request to the database and check for the existence of this model. But then we do the same in the controller (we get the model), these are 2 identical requests, which is not very correct. How can I pass the model to the controller from this class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hOtRush, 2019-02-21
@fgjcirifb

https://laravel.com/docs/5.7/routing#route-model-b...
https://github.com/lordthorzonus/php-dataloader#us...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question