lundi 14 mars 2016

Laravel 5.1 Where Condition In Many To Many relationships

Hello I'm very new in Laravel and, I'm having the following models :

organizations
-----------------
id
name

public function users()
{
    return $this->belongsToMany( 'App\User' );
}

users
-----------------
id
name

organization_user
------------------
user_id
organization_id

The there is a many to many relationship between organization and user. Now I want to find the organization where the user belongs to. How can I add a where condition for the user in the relationship.

For example I want to find all the organization of the logged in user. I'm trying the following :

$organisations       = Organisation::where('user_id' , '=' , $user->id)->paginate(10);

But its not working because user_id is in my Pivot Table. Please help and Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire