G
G
Garde22018-04-22 15:49:33
Laravel
Garde2, 2018-04-22 15:49:33

Where in Laravel to place duplicate requests?

Good afternoon. Sometimes there are cases when the SQL query almost completely or completely matches the one that was used before. Where to place this code and how best to reuse it?
Now, in the model I write something like this code

public static function selectDataReport($request)
    {
        return self::select('')
            ->leftJoin('')
            ->leftJoin('')
            ->with('test')
            ->where('id', '')
    }

And in the controller I already call
$data = MyClass::selectDataReport($request);
And I work with this request further
Is this method correct? And what is the best way to organize it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
danforth, 2018-04-22
@danforth

What you described looks like a model, only you need to pass inside not a request, but parameters, such as $id.
Regarding where the models are stored, it is written here:
In other words, you can store it in the app folder, but you are free to use another location for your models.

I
iljaGolubev, 2018-04-23
@iljaGolubev

If one model is a method in the model or scope.
If this is for different models, then my choice is copy-paste. Well, or a trait in the model.
If for these different models the same type of reports are built, then I use

class ReportRepository{
    function __construct(ReportableModelInterface $model){}
    function getSimpleReportData($id){}
    function getCustomReportData($id, $any){}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question