Php Laravel收集查询,我错在哪里?

Php Laravel收集查询,我错在哪里?,php,laravel,Php,Laravel,我的桌子看起来像这样 area_trip |id|dispatch_id|trip_id|status| equipment_trip |equipment_id|trips_id|dispatch_id| trips |id|dispatch_id|status 我正在尝试将集合传递给我的资源。有人可以检查我的查询并告诉我我做错了什么,因为下面的查询返回了所有与dispatch\u id匹配的数据,无论它是否与设备\u id匹配。顺便说一句,我是新来的拉威尔 return

我的桌子看起来像这样

area_trip

|id|dispatch_id|trip_id|status|

equipment_trip

|equipment_id|trips_id|dispatch_id|

trips

|id|dispatch_id|status
我正在尝试将集合传递给我的资源。有人可以检查我的查询并告诉我我做错了什么,因为下面的查询返回了所有与dispatch\u id匹配的数据,无论它是否与设备\u id匹配。顺便说一句,我是新来的拉威尔

return 
      Resources::collection(
        area_trip::where('dispatch_id', $request->dispatch_id)
    ->where('status', 1)
    ->orWhere('status', 9)

        ->whereHas('equipment_trip', function($query) use ($request) {
            $query->where('equipment_trip.equipment_id', '=', $request->equipment_id);
           })

            ->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
      ->orderBy('tripStartDate', 'ASC')
      ->orderBy('status', 'ASC')
      ->get());
以下是在area_trip model中建立的关系

public function equipment_trip()
{

    return $this->belongsTo(equipment_trip::class, 'trip_id', 'trips_id');

}

我相信您的whereHas子查询也不正确,而不是where和where use where in,您可以定义所有必要的状态,请尝试以下操作:

Resource::collection(area_trip::where('dispatch_id', $request>dispatch_id)
    ->whereIn('status', [1, 9])
    ->whereHas('equipment_trip', function($query) use ($request) {
            return $query->where('equipment_id', '=', $request->equipment_id);
    })
    ->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
    ->orderBy('tripStartDate', 'ASC')
    ->orderBy('status', 'ASC')
    ->get());

如何在其模型中设置关系???@ashish relationship添加到上面您可能想检查此处生成的查询以及
@lagbox的位置如何检查生成的查询?此外,我还尝试按照您的建议删除or,但现在没有任何结果。Tim仍然难以理解此处的预期结果。你能说得更准确些吗