Laravel Eloquent:如何从与函数模型的关系中添加条件

阿宾

有人可以帮助我吗?如何从与功能模型的关系中添加条件。

我试过了, ->where('driver.group_id',$group_id)但是没有用。请在下面查看我的代码。

public function getDriversDailyEarnings()
    {
        $group_id = Auth::user()->group->id;
        $today = Carbon::today();

        $data = DailyEarnings::with(['payout_cost',
        'status:id,name',
        'driver' => function ($query) {
            $query->with('user');
        }])
        ->where('driver.group_id',$group_id)
        ->whereDate('created_at', $today)
        ->get();

        return response()->json($data, 200);
    }
阿斯兰

你可以试试看 尚未测试。

DailyEarnings::with([
      'payout_cost', 
      'status:id,name'
      'driver' => function($query) {
         $query->where('group_id', auth()->user()->group->id)->with('user');
      }
   ])
   ->whereHas('driver', function($query) {
      $query->where('group_id', auth()->user()->group->id);
   })
   ->whereDate('created_at', now()->format('Y-m-d'))
   ->get();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章