user2992042
user2992042

Reputation:

laravel Eloquent where clause

I'm trying to do get all the users from a specific country via Eloquent.

The problem is that I get all the record, the where clause doesn't work.

$res = User::with(array('country' => function($query) {
          $query->where('country', '=', 'salope');
       }))->get();

Following this pattern from the laravel documentation

$users = User::with(array('posts' => function($query)
{
    $query->where('title', 'like', '%first%');
}))->get();

My models :

class User extends SentryUserModel {
    public function country() {

        return $this->belongsTo('country','country_id');
    }
}
class Country extends Eloquent {
    public function users() {

        return $this->hasMany('users');
    }

}

What am I doing wrong ?

Upvotes: 6

Views: 15785

Answers (5)

Andino Inyang
Andino Inyang

Reputation: 109

$matchThese = ['title' => post('title'), 'slug' => post('slug')];
return Blog::where($matchThese)->get();

Something like this works too

Upvotes: 1

Tobiushirogeri
Tobiushirogeri

Reputation: 11

This worked for me as of Laravel 5.6

I needed to create a relation for users with their roles. The relations has a pivot key using the user_roles tables that is linked to the role table.

In my User model

public function role(){
        return $this->belongsToMany('App\Roles', 'user_roles','user_id','role_id');
    }

Then, I created a static function in the User model

  public static function withRole($role){
        $members = self::whereHas('role',function($q) use($role){
            $q->where('name', '=', $role);
        })->get();
       return $members;
    }

Note the use($role) usage in the function.

I can then call this method with :

$members = User::withRole('the_role_name');

Hope this helps someone !

Upvotes: 0

user2992042
user2992042

Reputation:

I found what I was looking for with laravel 4.1 version. My question was not formulated correctly. I wanted to query a relationship.

$posts = Post::whereHas('comments', function($q)
{
    $q->where('content', 'like', 'foo%');

})->get();

so with my exemple:

 $res = User::whereHas('country', function($q) {
        $q->where('country', 'salope');
       })->get();

Documentation link

Upvotes: 4

user1669496
user1669496

Reputation: 33108

The problem is you are retrieving all the users and then limiting only the countries those users can have. So you are going to end up with all the users still, and not just the ones that belong to Salope.

I think the solution is to go about this backwards.

$country = Country::where('name', 'salope')->with('users')->first();

foreach($country->users as $user)
{
    echo $user->username;
}

Upvotes: 2

afarazit
afarazit

Reputation: 4984

If you want the users for a particular country you need something like that.

$users = Country::whereCounty('salope')->users;
dd($users);

Where users is an eloquent Collection and you loop over that Collection and display the users. If you would like to continue building your query based on your users use users() and keep chaining, for example:

$users = Country::whereCounty('salope')->users()->whereBanned(0)->get(); // Or something
dd($users);

Upvotes: 2

Related Questions