php - creating json api laravel belongstomany relationship -


i trying build api laravel, have shop, comment, user, food , shop_food table. want api show list of shops, each shop information, 5 foods, comments on shop , user gave comments.

<?php  namespace app;  use app;  use illuminate\database\eloquent\model;  class shop extends model  {     /**      * table associated model.      *      * @var string      */     protected $table = 'shops';  /**  * attributes mass assignable.  *  * @var array  */  protected $fillable =  ['name','description','address','district','city','country','lat','long','speciality'];    /**  * shop associated location.  */ public function comments() {     return $this->hasmany('app\comment'); }  /**  * shop associated shops.  */ public function foods() {     return $this->belongstomany('food', 'shop_food',         'shop_id', 'food_id'); }    } 

my shop controller

<?php  namespace app\http\controllers;  use app\shop; use app\comment; use illuminate\http\request;  class shopcontroller extends controller {     /**      * display listing of resource.      *      * @return \illuminate\http\response      */     public function index()     {         $shops = shop::with('comments.user')->get();         return response(['shops' => $shops->toarray(), 'error'=>'false'], 200);     } 

what need please help. right cos of errors able display shop->comments->user need add food api

enter image description here

if relation between shops , foods available in methods... can this:

$shops = shop::with('comments.user', 'foods')->get(); 

this way multiple relationships , when $shop->toarray() show in api... right?


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -