Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FareedR's avatar

eloquent whereDoesntHave()

how can i find company project that doesnt have any pics(person in charge) based on their category name yet ? am i doing it correctly?

$project = CompanyProject::whereHas('company', function ($query) use ($setting_industry){
                    $query->where('category',$setting_industry->name);
                })->whereDoesntHave('pics')->where('is_approved',1)->first();
0 likes
2 replies
Snapey's avatar

what has pics ? the company or the project?

If you leave that bit out, do you get projects from the right type of companies?.

TutanRamon's avatar

Besides the whereExists method, there is also an whereNotExists method which you can use to check if the item in table A doesn't exist in table B.

https://laravel.com/docs/7.x/queries#where-exists-clauses (although whereNotExists isn't described there, it is a good starting point to look further).

EDIT: there is also a topic about this on Stackoverflow with some good examples: https://stackoverflow.com/questions/50711923/where-not-exists-en-laravel

Please or to participate in this conversation.