简体   繁体   中英

Chaining Eloquent queries in Laravel

I cannot figure out what would be the best / DRY way to chain a few model methods. For example, I have a ProjectRepository class that I inject in my ProjectsController . The repo class has a method:

public function featured()
{
   return $this->model->featured()->order('order')->get();
}

In here, $this->model is a Project model injected in the __constructor , and that featured() in the query is just a scopeFeatured() set on the model itself.

In some cases, I don't want that featured method on my repo to return items in order at all, so what I want to achieve is something like:

public function featured() {};

public function ordered() {};

and then to be able to do something like

return $this->repo->featured()->ordered() ;

Is it possible to do something like that?

Nevermind. Found this useful post and it is exactly what I needed:

http://culttt.com/2014/03/17/eloquent-tricks-better-repositories/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM