Only need one relation column in get() laravel eloquent


Only need one relation column in get() laravel eloquent



I only want to pluck 1 column of relation.



example: If I want unique names of authors according to input for Paper table



Paper has many author is the relation


$paper = Paper::with([
'author' => function($query) use($request)
{
$query->where('name', 'LIKE', '$request->name');
}

])->pluck('name')->unique();



But this simply gives error as name is not recognized as the column of Paper.





Why the <sql> tag?
– jarlh
Jun 29 at 9:57





@jarlh if it could be solved with DB facade of laravel...
– Raj
Jun 29 at 10:02






Do you want an SQL answer?
– jarlh
Jun 29 at 10:05





Do you only need the author names? Or the papers and the author names?
– Jonas Staudenmeir
Jun 29 at 10:19





Use has(): Author::has('papers')->where('name', 'LIKE', $request->name)->pluck('name')->unique();
– Jonas Staudenmeir
Jun 29 at 12:30



has()


Author::has('papers')->where('name', 'LIKE', $request->name)->pluck('name')->unique();









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift