Posts

Showing posts with the label eloquent

One to Many to One relationship in laravel eloquent

One to Many to One relationship in laravel eloquent I have company , employee and motorCycle table. company employee motorCycle One Company has many employee. One employee has One motorCycle Company.php <?php namespace AppModel; use IlluminateDatabaseEloquentModel; class Company extends Model { protected $table = 'company'; protected $primaryKey = '_id'; public function employee() { return $this->hasMany(Employee::class); } } ?> Employee.php <?php namespace AppModel; use IlluminateDatabaseEloquentModel; class Employee extends Model { protected $table = 'employee'; protected $primaryKey = '_id'; public function motorCycle() { return $this->hasOne(MotorCycle::class, 'motorCycle', 'id'); } } ?> MotorCycle.php <?php namespace AppModel; use IlluminateDatabaseEloquentModel; class MotorCycle extends Model { protected $table = ...

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