laravel 如果模型返回非空,则有条件调用关系[已关闭]

pgpifvop  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(182)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
4天前关闭。
Improve this question
我有以下Eloquent查询:

$presupuesto = Departamento::where([
        ['fk_presupuesto', '=', $request['periodo']],
        ['fk_programa', '=', $request['programa']['id_catalogo']],
        ['fk_departamento', '=', $request['departamento']['id_catalogo']],
    ])
        ->with([
            'bienes' => function ($query) use ($request) {
                $query->select([
                    'fk_departamento',
                    'costo_total'
                ])->where([
                   ['fk_presupuesto', '=', $request['periodo']]
                ]);
            }
        ])
        ->first();

Query works great...当where条件返回非null时,否则Laravel会向我大喊服务器错误,我猜这是因为在null对象上调用with

Laravel响应

如何检查模型是否返回非空值?或者我可以将整个关系和约束移动到load方法中吗?

toe95027

toe950271#

事实证明,这不是with()关系调用给出的错误,而是阅读日志(感谢Tim刘易斯的提醒),这是我返回最终响应的方式,错误如下:
[2023-05-12 13:48:22]本地。错误:调用成员函数toArray()on null {“exception”:“[object](Error(code:0):在null上调用成员函数toArray()
查询返回null,我试图返回null->toArray()。

相关问题