php - Get Table values using Eloquent Model in Laravel5 -
i want find username post_id using post_tag table using eloquent model. here tables.
user table
user_id(pk) username post table
post_id(pk) user_id(fk) post_tag table
post_id(fk) tag_id(fk) how can fetch username user table using post_tag table. here have tried:
class post extends eloquent { public function user() { return $this->belongsto('app\user'); } } class posttag extends eloquent { public function post() { return $this->hasmany('app\post'); } } $posts = posttag::with('post')->where('tag_id','=','20')->get(); one tag belongs multiple posts , each post belong owner/user. thank in advance.
if relations working it:
posttag::find(20)->post()->first() ->user()->first()->username; you 1 tag belongs many posts, how choose of many posts want user's username?
do want users have post posttag?
edit: try this:
posttag::find(20)->with(['post', 'post.user'])->get(); run in artisan tinker or dd result can see returned, think solves problem. note may return lot of data, use take instead of get limit results.
Comments
Post a Comment