具有laravel关系的Sql的最佳方法是什么?

  • Manager hasOne Vendor
  • Vendor hasMany Menu

And my route like this:

Route::get('vendor/{vid}/menu/{id}', 'MenuController@show');

I already wrote a query like this:

   $vendor = manager::where('status','>',0)
   ->with(['vendor' => function($query) use ($vid) {
        $query->where('id',$vid);
   }])->first()->vendor()->first();

   $menu= $vendor->menu()->where('id',$id)->first();

   return $menu;

I don`t Know the original Sql by this written ,but It seem query manay time, is that any way for best efficacy?

You could simplify the code, as it's equivalent of:

$menu = vendor::whereHas('manager', function($q) {
  $q->where('status', '>', 0);
})->findOrFail($vid)->menu()->find($id);

A call to findOrFail() will return an error if given vendor cannot be found. whereHas will make sure that related manager has correct status and find() will return a single model by default.