laravel - Laravel5 Eloquent Query -


i new laravel , stuck query. have shopcustomer table shop_id , customer_id in it.there multiple entries of shop_id , different customer_id in it. has many many relationship.

i have shop_id = 2. want find customers name = 'mat'. have tried code join clause. looking convert following query eloquent query builder , want use eager loading.

$customers = \db::table('shop_cust')             ->join('customers', 'customers.customer_id', '=', 'shop_cust.customer_id')             ->select('shop_cust.total_amount', 'customers.*')             ->where('customers.name', 'like', 'mat')             ->where('shop_cust.shop_id', '=', '2')             ->get();  dd($customers); 

this have tried.

$customers = shopcust::with(['customer' => function ($query)  use ($customername) {       $query->where('name', 'like', '%' . $customername . '%');  }])->where('shop_id','=', $shopid)->get(); 

shopcustomer model

class shopcustomer eloquent {      public function customers()     {     return $this->hasmany('customer','customer_id','customer_id');     }       public function shops()     {     return $this->hasmany('shop','shop_id','shop_id');     }  } 

customer model

class customer eloquent {      public function customer_shop()     {     return $this->belongtomany('shopcustomer','customer_id','customer_id');     }  } 

shop model

class shop eloquent {          public function shop_customer()         {         return $this->belongtomany('shopcustomer','shop_id','shop_id');         }      } 

your query

$customers = shopcustomer::     ->with(arra('customer' => function($q) use ($customername){                                         $query->where('name', 'like', '%' . $customername . '%');                                     }))     ->where('shop_id','=', $shopid)->get(); 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -