php - Laravel Eloquent belongs to many through -



php - Laravel Eloquent belongs to many through -

i have 3 tables:

users (id, name) tasks (id, title) task_user (id, task_id, user_id, user_post_id) user_posts (id, title)

what trying fetch users related project.

in task model have:

class task extends \eloquent { protected $guarded = []; public function users() { homecoming $this->belongstomany('\user')->withpivot('user_post_id', 'hours'); } }

and is working pretty well. getting result:

[0] => array ( [id] => 9 [created_at] => 1996-08-04 08:35:59 [updated_at] => 1991-07-27 16:36:47 [username] => xcremin [email] => amely.wunsch@gmail.com [remember_token] => $2y$10$wmtdoccia25z/cf28kqlwongr5vhvghd3cu0bbtpgftuv/ez2wbge [pivot] => array ( [task_id] => 1 [user_id] => 9 [user_post_id] => 10 ) )

here can see pivot user_post_id field. question - how fetch title of field (with user_post_id = 10)?

you have add together relation in taskuser class

public function userpost() { homecoming $this->belongsto('userpost'); }

and after you''ll able name doing in view

@foreach($task->users $user) {{ $user->pivot->userpost->title }} @foreach

php laravel eloquent

Comments

Popular posts from this blog

javascript - THREE.js reposition vertices for RingGeometry -

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

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