Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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();
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rbruhn rbruhn Joined 16 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.