Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

Hello,

I try to find how to have only result where the relation return something, not when there empty.

I try this :

$c = Produit::whereNotIn('id_dispo', array(Statut_Dispo::FERME, Statut_Dispo::VENDU))
						->with( array ('categorie' => function ($query) use ($id) {
								$query->where('categorie.id_categorie' , $id);
						}))
						->get();

Return :

{
"id_produit":"21870",
"reference":"21870",
"created_at":"2014-08-05 21:50:06",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null,
"prix":"50",
"prixha":null,
"qte":"1",
"stock_initial":"1",
"id_dispo":"2",
"featured":"0",
"id_utilisateur_creation":"6",
"id_utilisateur_modification":null,
"id_lot":"1",
"id_taxe":"1",
"hash":null,
"km":"53242",
"categorie":[
]
},

How to do if i don't want have a result when "categorie" is empty ?

Last updated 3 years ago.
0

Hi.

What you need is whereHas : http://laravel.com/docs/eloquent#querying-relations

Last updated 3 years ago.
0

Oh nice Thank you

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

jeremy379 jeremy379 Joined 29 Jul 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.

© 2025 Laravel.io - All rights reserved.