Reputation: 29019
I have the following model:
class User extends Model {
public function addresses() {
return $this->hasMany('Addresses');
}
public function billAddress() {
return $this->addresses()->where('id','=', $this->idBill);
}
}
The following query will result in an empty collection.
\App\User::has('billAddress')->get();
However, any instance of a user has a billAddress
.
Is it simply not possible to use $this->attribute
in a hasMany relation? Or is there some other way how to achieve this?
Upvotes: 0
Views: 553
Reputation: 737
\App\User::has('billAddress')->get();
results in an empty collection, because \App\User
is not one particular user, but the actual User Model.
Assuming that you have an existing User in your database and that $idBill
is defined in the User Model, you can get the 'billAdresses' relation like this:
$user = User::find(1); // just an example, but it has to be a User Model
$billAddresses = $user->billAddress->get(); // the result your are looking for
foreach ($billAdresses as $billAddress) {
// your code
}
Upvotes: 1