I want to see if a model id has a related id in a related table. I have Shipping Profile id 1, and want to check if it has a related Method id 5.
$profile = \App\ShippingProfile::find(1)
->whereHas('methods', function($query) {
$query->where('id',5);
})->first();
return $profile;
From this I get back the Profile 1 - and since that profile has a method of 5, it seems correct. But if I change where
to 7
, it gives me profile id 2
which I didn't even ask for!
The result gives me whatever profile is related to the method ID I specify in the where
clause . Very strange.
The relationship on the ShippingProfile
model:
public function methods() {
return $this->hasMany('\App\Method');
}
The relationship on the Method
model:
public function profile() {
return $this->belongsTo('\App\ShippingProfile', 'shipping_profile_id');
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire