User : id,name,age
Shop : id,user_id,name
Address : id, shop_id, address
Shop Type : id, shop_id, type
A [user] has multi [shop], and the [shop] has multi branch, so it has multi [address], and the [shop] also has multi [type] such as alcohol,food,snack,drink and more.
Now i want get the user's shop with all address and shop type.
In model i use User Class public function shop(){ return $this->hasMany('App\Shop'); }
Shop Class public function address(){ return $this->hasMany('App\Address'); }
public function type(){
return $this->hasMany('App\ShopType');
}
Address Class public function state(){ return $this->hasMany('App\State'); }
public function city(){
return $this->hasMany('App\City');
}
public function country(){
return $this->hasMany('App\Country');
}
My Control
public function shop($id)
{
$shop = User::where("id",$id)->with('shop.address','shop.type')->first();
if($shop){
return response()->json(
[
'shop' => $shop->shop,
],
200,
array(),
JSON_PRETTY_PRINT
);
}else{
return false;
}
Code Above can get all the shop's address and shop's type in the database, but how can i do filter only shop's type = 'food' and 'drink' and country code is us with programming? i try code below, but not work for me :
$type = {'food','drink'}; // Example
$user = {'1'}; // Example
public function shopWithFilter($id,$type,$country)
{
$shop = User::where("id",$id)->with('shop.address','shop.type')->where(['shop.type.name'=>$type,'shop.address.country.code',$country])->first();
if($shop){
return response()->json(
[
'shop' => $shop->shop,
],
200,
array(),
JSON_PRETTY_PRINT
);
}else{
return false;
}
Thanks
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire