Laravel Eloquent - many to many with where for the pivot table -



Laravel Eloquent - many to many with where for the pivot table -

i'm trying see best way following.

i have 3 tables: users, items , item_user.

users , items table pretty generic, id , few columns hold whatever data.

item_user table has next construction id item_id user_id user_type [ 1 - owner | 2 - follower | 3 - else ]

relationships: each item has 1 owner (user type) each item has many followers each user can own many items each user can follow many items

i have owner , followers users table don't need replicate user data. created pivot table of item_id, user_id , user_type hold these relationships.

so question how in laravel eloquent?

item model looks like:

public function user() { homecoming $this->belongstomany('users'); // isn't right since belongs 1 user not sure how specify user_type = 1; // homecoming $this->belongsto('user'); }

user model looks like:

public function item() { homecoming $this->hasmany('item'); }

thanks!

you can append status belongstomany declaration:

public function user() { homecoming $this->belongstomany('users')->where('user_type', 1); }

this homecoming user entries have user_type = 1 in pivot table. , create more clear name method owner() instead of user() reflect added condition.

laravel-4 eloquent

Comments

Popular posts from this blog

javascript - I need to update the text of a paragraph by inline edit -

javascript - THREE.js reposition vertices for RingGeometry -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -