简体   繁体   中英

Laravel 5 filter main data with relationship

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. Following are my models:

User model

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

Problem Solved, below is my answer:

public function shopWithFilter($id,$type,$country)
{
    $shop = User::where("id",$id)->with('shop.address','shop.type')
    ->whereHas('shop.address' function($q) use($country){
        $q->where('name',$country);
    })
    ->whereHas('shop.type' function($q) use($type){
        $q->where('name',$type);
    })
    ->first();
    if($shop){
        return response()->json(
            [
                'shop' => $shop->shop,
            ],
            200,
            array(),
            JSON_PRETTY_PRINT
        );
    }else{
        return response()->json(
            [
                'shop' => null,
            ],
            200,
            array(),
            JSON_PRETTY_PRINT
        );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM