First off, are you sure you need Projects-Subjects relation? If Subject is part of Expedition, then I suppose you don't.
Nevertheless you can do this:
// return Eloquent Collection of Subject models having no relation with Expeditions
// for the project with id $projectId
Subject::has('expeditions','<',1)->where('project_id',$projectId)->get();
Thought I would update this as it no longer works. Might have been a change in Laravel. Still hunting down the problem as I have not changed this portion of my code for quite some time.
return Subject::has('expedition','<', 1)->where('project_id', '=', $projectId)->count();
This results in the following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'subjects.id' in 'where clause' (SQL: select count(*) from `expeditions` inner join `expedition_subject` on `expeditions`.`id` = `expedition_subject`.`expedition_id` where `expedition_subject`.`subject_id` = `subjects`.`id`)
Both Expedition and Subject has belongsToMany() relationships defined correctly.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community