Laravel 5, get only relationship for eloquent model -
i have user , article models in app. relation between them straightforward:
//article.php use taggable; public function user() { return $this->belongsto('app\user'); }
article uses taggable lib provides me variety of methods article::withanytags (returns articles tagged 'xyz' string). i'd users posted article/s tagged 'xyz'. know how in 2 lines tells me not right. ideally i'd sth like:
$users = article::withanytags('xyz')->with('user')->select('user'); --pseudocode
is possible sth in eloquent?
side note: knot db::table not option me. please note there no "->get()" @ end on pseudocode. it's so, because i'm paginating users' results set lib works eloquent queries.
you use wherehas():
user::wherehas('articles', function ($query) { $query->withanytags('xyz'); })->paginate();
or if need pass variable of tags closure do:
$tag = 'xyz'; user::wherehas('articles', function ($query) use($tag) { $query->withanytags($tag); })->paginate();
hope helps!
Comments
Post a Comment