Laravel 5 belongsToMany如何获取方法all()中的数据;

时间:2015-09-02 16:24:43

标签: laravel-5 relationship

我以前属于ToMany关系 我试过了 我的Resta模特

public function resto()
{
    return $this->belongsToMany('App\Resto');
}

我的厨房模型

public function getAllRest($city)
{
    $restos = Resto::latest('created_at')
        ->where('city','=', $city)
        ->get();

    return $restos;
}

返回Resto数据的功能

 $resta = Resto::find($id);
 $resta->kitchens->toArray();

现在我有一个接口关系,因为它们输出? 在一个我知道可以这样做

protected def find[U <: T](t: U): Query[Table[_], U, Seq]

如何输出所有数据的推导?

1 个答案:

答案 0 :(得分:0)

解决! 添加这个

$restos = Resto::latest('created_at')
    ->where('city','=', $city)
    ->get();
foreach($restos as $resto){
   $resto->kitchens->toArray();
}

return $restos;