0
votes

Hi guys i am using laravel5.5 I have two tables Users and Services

Users Table

  1. id
  2. name
  3. email
  4. password
  5. address
  6. city
  7. country
  8. zipcode

Service table

  1. id
  2. User_id
  3. name
  4. description
  5. price

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.

2

2 Answers

1
votes
$services = Service::with(['user'])->whereHas('user', function($q) {
                 $q->where('zipcode', '20006');
            })->get();
0
votes

Finally got it

$services = Service::whereHas('user', function($q)use($zipcode) {
                $q->where('zipcode' , $zipcode);
            })->get();