Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩:如何从函数模型关系中添加where条件_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php Laravel雄辩:如何从函数模型关系中添加where条件

Php Laravel雄辩:如何从函数模型关系中添加where条件,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,有人能帮我吗?如何从函数模型关系中添加where条件 我尝试了->where('driver.group\u id',$group\u id),但它不起作用。请看下面我的代码 public function getDriversDailyEarnings() { $group_id = Auth::user()->group->id; $today = Carbon::today(); $data = DailyEarning

有人能帮我吗?如何从函数模型关系中添加where条件

我尝试了
->where('driver.group\u id',$group\u 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);
    }

要将其他子句传递给关系,只需在数组中使用它,其中键、关系名称和值是接收查询实例的函数,如下所示:

public function getDriversDailyEarnings()
{
    $data = DailyEarnings::with([
        'payout_cost' => function ($myWithQuery) {
            $myWithQuery->where('column-of-relation-here', 'value-here')
                ->orWhere('name', 'LIKE', '%Steve%');;
         }
    ])->get();

    return response()->json($data, 200);
}
  //        ->where('driver.group_id',$group_id)
            ->whereHas('driver', fn($qry) => $qry->where('group_id', $group_id)) // if group_id a field on driver

也许是这样的:

public function getDriversDailyEarnings()
{
    $data = DailyEarnings::with([
        'payout_cost' => function ($myWithQuery) {
            $myWithQuery->where('column-of-relation-here', 'value-here')
                ->orWhere('name', 'LIKE', '%Steve%');;
         }
    ])->get();

    return response()->json($data, 200);
}
  //        ->where('driver.group_id',$group_id)
            ->whereHas('driver', fn($qry) => $qry->where('group_id', $group_id)) // if group_id a field on driver

你可以试试这个。我还没有测试过

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();

这是工作,但我更新到这个<代码>->whereHas('driver',function($query)use($group\u id){$query->where('group\u id',$group\u id);})。谢谢,这又好又短。它也起作用。。但是我的编辑器:VS代码检测到此代码中的错误
fn($qry)=>$qry->