laravel - 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();
解决方案
我认为您可以使用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();
推荐阅读
- typescript - 如何在 TypeScript 中的“任何”类型上创建扩展方法?
- python - 如何将 SQLAlchemy 相关代码拆分为模块
- c - 如果已声明但未初始化字符串,是否会给它们一个内存地址?我需要帮助了解数据恢复练习中的一些奇怪之处
- mysql - 如何在 PHP Laravel 中的几个 id 下获取单行的所有值?
- python - 如何加快将 numpy 数组值分配给 Mesa 中的对象(代理)
- css - css动画翻转没有后翻转
- python - 在 Python 中需要继承?
- python - 有没有办法创建一个字符列表,将它们 ord() 转换为整数,并将 hex() 转换为十六进制代码?
- listview - 在编辑单击时使用对话框编辑 Listview 项目
- c++ - 如何使用可视化代码保存 C++ 代码文件?