ahmedalahmed's avatar

How to use repository in complex queries ?

Hello, We use repositories to abstract the database logic okay ?? I have a simple interface for my repository like this one.

public function create($data = array());

    public function deleteWhere($conditions = array(), bool $first = true);

    public function updateWhere($conditions = array(), $data = array(), bool $first = true);

    public function findWhere($conditions = array(), $columns = array('*'), bool $first = true);

    public function all(bool $pagination = false, $columns = array('*'), $perPage = 20, $with = array());

so suppose I have a complex query like

$q->where(...)
->orWhere(function($q) { ... } )
 ->whereHas(....)
-> ...

how to abstract this one in the best way , I mean how to write the above code using repository concept in the best way.

0 likes
3 replies
Sergiu17's avatar

Hi, every single Model has a Repository, just add a simple method with a name that describes the query, and you are good to go.

In your Interface - should go only general methods like findAll(), findById(), deleteById(), basic / general functions

Please or to participate in this conversation.