S
S
Sergey Popov2018-09-02 13:54:48
Laravel
Sergey Popov, 2018-09-02 13:54:48

How to hide complex query in eloquent model?

Good afternoon.
It is necessary to hide a lot of complex queries to the database in simple methods of the model. It concerns many models, so there are no options to create a query builder for each.
Example.

public function available() {
    return $this->where('id', 1)
}

public function withSomeMore($l) {
    return $this->whereHas('smth', function ($q) use($l) {
        $q->where('q', 1);
    }
}

// Вызов функции
$model->available()->withSomeMore()->get();

How can such functionality be implemented? This damn thing does not work, since calling the where, whereHas and any other method will return a QueryBuilder.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2018-09-02
@be_a_dancer

I assume you need scopes? https://laravel.com/docs/5.5/eloquent#query-scopes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question