Reputation: 141
Hi guys i am using laravel5.5 I have two tables Users and Services
Users Table
Service table
In User Model
public function services()
{
return $this->hasMany('App\Service');
}
In Service Model
public function user()
{
return $this->belongsTo('App\User');
}
Now in controller I need all services where user->zipcode = 20006 How can i get it I have tried this below code
$services = Service::with('user')->where('user->zipcode', '20006')->get();
But it did not work.
thanks in advance. Warm Regards: Abdullah Shahid.
Upvotes: 0
Views: 36
Reputation: 141
Finally got it
$services = Service::whereHas('user', function($q)use($zipcode) {
$q->where('zipcode' , $zipcode);
})->get();
Upvotes: 0
Reputation: 802
$services = Service::with(['user'])->whereHas('user', function($q) {
$q->where('zipcode', '20006');
})->get();
Upvotes: 1