laravel 在最后 child 上嵌套了带有 where 子句的 hasmany

laravel nested hasmany with where clause on last child

我想从最后一个 child 的 where 子句的嵌套关系中获取数据(作为 json 嵌套响应)。我正在用 4 个嵌套模型制作一个 API,这些模型有很多关系,如下所示: modelA ->hasmany modelB ->hasmany modelC ->hasmany 模型 D (modelA 是第一个 parent 等等。)

我在 api.php

上有路线
Route::get('/rdata/{string}', [ModelAController::class, 'getdata']);

在我的 ModelAController 上:

public function getdata($string)
{
    return ModelA::thedata($string);
}

然后在我的 ModelA 上

public static function thedata($string)
{ 
    return ...
}

我想根据第 4 个 ModelD 的字段获取数据。我怎样才能在 4 日做一个 where 子句 child 可能是这样的:

where('column', 'like', '%'.$string.'%')

我也尝试过使用集合资源,我可以很好地嵌套所有数据,但我无法在最后一个 child 上执行我想要的查询。我取得的最好成绩是在最后 child 加载 parent 时进行查询,但它无法正常工作

return ModelD::where('column', 'like', '%'.$string.'%')->with('modelc.modelb.modela')->get();

甚至有负载

return ModelD::where('column', 'like', '%'.$string.'%')->get()->load('modelc.modelb.modela');

然后在 ModelC 上:

public function modelb()
{
    return $this->belongsTo(ModelB::class, 'f_key','f_key');
}

和其他 parents 一样,但这是错误的,因为我再次将所有 children 放入其中,并且 json 被反转。我想保留格式:

 $response = ['ModelA' => [
                            'id' => '..'
                            'field1' => '..'
                            'ModelB'=>[
                                etc..
                            ]]

或者还有其他我看不到的方法吗?在某种程度上,我需要这样的东西,但格式为嵌套 json

return DB::table('modela')
->join('modelb', 'modela.id', 'modelb.modela_id')
->join('modelc', 'modelb.id', 'modelc.modelb_id')
->join('modeld', 'modelc.id', 'modeld.modeld_id')
->where('column', 'like', '%'.$string.'%')
->get();

Edit1:到目前为止,我根据答案得到了这个,我想在最后 child

上获得 parent 列
return self::with('modelb.modelc.modeld')-> whereHas('modelb.modelc.modeld', function ($modeld) use ($string) {
    $modeld->where('column', 'like', "%$string%");
    ->whereColumn('modelc.column2', 'modeld.column2')
    ->whereColumn('modelc.column3', 'modeld.column3');
})
->with(['modelb.modelc.modeld' => function ($modeld) use ($string) {
    $modeld->where('column', 'like', "%$string%");
    ->whereColumn('modelc.column2', 'modeld.column2')
    ->whereColumn('modelc.column3', 'modeld.column3');
}])
->get();

我想你可以用 whereHas/with 闭包来做到这一点。

ModelA::query()
    ->whereHas('modelb.modelc.modeld', function ($modeld) use ($string) {
        $modeld->where('column', 'like', "%$string%");
    ->with(['modelb.modelc.modeld' => function ($modeld) use ($string) {
        $modeld->where('column', 'like', "%$string%");
    }])
    ->get();

类似这样,但条件可能更精细。


我认为根据您的编辑,此查询应该可以满足您的需求。

ModelA::query()
    ->whereHas('modelb.modelc', function ($modelC) use ($string) {
        $modelC->whereHas('modeld', function ($modelD) use ($string) {
            $modelD->whereColumn('table_c.column2', 'table_d.column2')
                   ->whereColumn('table_c.column3', 'table_d.column3')
                   ->where('table_d.column', 'like', "%$string%");
        });
    ->with(['modelb.modelc' => function ($modelC) use ($string) {
        $modelC->with(['modeld' => function ($modelD) use ($string) {
            $modelD->whereColumn('table_c.column2', 'table_d.column2')
                   ->whereColumn('table_c.column3', 'table_d.column3')
                   ->where('table_d.column', 'like', "%$string%");
        }]);
    }])
    ->get();