Ich versuche, eine API mit Laravel zu bauen, ich habe einen Shop, Kommentar, Benutzer, Essen und shop_food Tisch. Ich möchte, dass die API eine Liste von Geschäften zeigt, jede mit Shop-Informationen, 5 Lebensmitteln, Kommentare zum Shop und Nutzer, der Kommentare abgegeben hat.Erstellen von JSON Api Laravel hingestomany Beziehung
<?php
namespace App;
use App;
use Illuminate\Database\Eloquent\Model;
class Shop extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'shops';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name','description','address','district','city','country','lat','long','speciality'];
/**
* Get the shop associated with the location.
*/
public function comments()
{
return $this->hasMany('App\Comment');
}
/**
* Get the shop associated with shops.
*/
public function foods()
{
return $this->belongsToMany('Food', 'shop_food',
'shop_id', 'food_id');
}
}
Mein Geschäft Controller
<?php
namespace App\Http\Controllers;
use App\Shop;
use App\Comment;
use Illuminate\Http\Request;
class ShopController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$shops = Shop::with('comments.user')->get();
return response(['shops' => $shops->toArray(), 'error'=>'false'], 200);
}
, was ich tun müssen, tun Sie bitte helfen. Gerade jetzt cos der Fehler bin ich nur in der Lage Shop-> Kommentare-> Benutzer anzuzeigen, aber ich muss hinzufügen Nahrung zum api