jeudi 24 septembre 2015

Laravel 5 get user with relationship‘s and multi relationship

Database Below

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');
}

My Control

public function user($id)
    {
            $user = User::where("id",$id)->with('shop.address')->first();
    if($user){
            return response()->json(
                [
                    'user' => $user,
                ],
                200,
                array(),
                JSON_PRETTY_PRINT
            );
    }else{
            return false;
    }

Code above can get all user's shops and shops's address but how can i get also shops's type?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire