Support the ongoing development of Laravel.io →
Database Eloquent Architecture

Having trouble finding the way to use Query Scopes with Repositories. Is there a way?

Would like to use for basic scoping as in the example.

http://laravel.com/docs/eloquent#query-scopes

// Model based example from docs...
class User extends Eloquent {

    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

    public function scopeWomen($query)
    {
        return $query->whereGender('W');
    }
}

$users = User::popular()->women()->orderBy('created_at')->get();

Example using Repositories instead of Models?

Last updated 3 years ago.
0

What do you mean by Repositories instead of Models ?

class UserRepository{

  public function getPopularWomen(){

    return User::popular()->women()->orderBy('created_at')->get();

  }

}

:)

That could be useful for testing (by injecting repositories in the controllers). But repositories are often associated with the data mapper design pattern, which has a different way of handling data than active record (Eloquent's design pattern). Take a look at Doctrine if you're interested in the data mapper pattern.

Last updated 3 years ago.
0

It's not difficult. I just do something like this: https://gist.github.com/thepsion5/eb0af8b3040eaa0b219e

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jhauraw jhauraw Joined 12 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.