Posts

Showing posts with the label eloquent

Multilevel relationship whereHas on eloquent Model in Laravel

Multilevel relationship whereHas on eloquent Model in Laravel I'd like to find Project::with('tasks.tags')->get(); Project::with('tasks.tags')->get(); where only projects with a particular id of tag return in the result set. For ex. I'd like to find a project with tasks and tasks with tags with only id of 1. In other words, filter the tasks return inside of the Project - Task relationship. I have tried various ways but have failed so far. I have tried: $project = Project::with('tasks.tags')->whereHas('tasks', function($query){ $query->whereHas('tags', function($query) { $query->where('id', 1); }); })->get(); And: $project = Project::with('tasks.tags')->whereHas('tasks', function($query){ $query->whereHas('tags', function($query) { $query->where('tag_id', 1); }); })->get(); This is how the re...

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 ...