首页 > 解决方案 > laravel 嵌套 hasmany 与最后一个孩子的 where 子句

问题描述

我想从带有最后一个孩子的 where 子句的嵌套关系中获取数据(作为 json 嵌套响应)。我正在制作一个包含 4 个嵌套模型的 API,这些模型的关系如下:modelA ->hasmany modelB ->hasmany modelC ->hasmany modelD(modelA 是第一个父级等)

我在 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 的字段获取数据。我怎样才能对第四个孩子做一个 where 子句可能是这样的:

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

我也尝试了收集资源,我可以很好地嵌套所有数据,但我无法对最后一个孩子执行我想要的查询。我取得的最好成绩是对最后一个孩子的父母进行查询,但它不能正常工作

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');
}

其他父母也是如此,但这是错误的,因为我再次将所有孩子都放入其中,而且 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:到目前为止,我根据答案得到了这个,我想获得最后一个孩子的父列

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

标签: laraveleloquent

解决方案


我认为您可以使用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();

推荐阅读