php - 基于关系 laravel 过滤模型
问题描述
所以我的代码是
$categories=Category::where('user_id', $store->id)->whereHas('childrenCategories', function($q) use ($id){
$q->where('user_id', $id);
})->orwhereHas('products', function($q) use ($id) {
$q->where('auth_id', $id);
})->with('products', 'childrenCategories')->latest()->get();
我想获取所有具有给定 ID 的子类别和产品,但此代码似乎不起作用。作为具有除 id 之外的 user_id 的子类别也被返回。抱歉,我对 Laravel 比较陌生。我认为这将是一个很好的提问平台。另外,如果您愿意,我可以分享这些关系。
解决方案
I solved this with the following. Thanks, @lagbox for your time.
$categories = Category::with(['childrenCategories' =>
$childrenClosure = function ($query) use ($id) {$query->where('user_id', $id);}, 'products'
=> $productsClosure = function ($query) use ($id) {$query->where('auth_id', $id);}])
->where('user_id', $store->id)
->where(function ($query) use ($childrenClosure, $productsClosure) {$query->whereHas('childrenCategories', $childrenClosure)
->orWhereHas('products', $productsClosure);})
->latest()
->get();
推荐阅读
- database - 计算符合条件的数组元素
- c# - 是否有一种有效的方法来确定 MeshGeometryModel3D 的任何顶点是否在视口中可见?
- html - 图像、标题和文本的对齐
- c++ - 性能差异:std::accumulate vs std::inner_product vs Loop
- field - 如何获取选择字段的特定项目。奥多 8
- php - 为什么 php 标签内的内容在 index.php 内有问题
- navision - 时间格式
在 Microsoft Dynamics Navision 2009 中始终为零 - python - Django Rest 框架,如何使用 foreignkey_id 字段包含“__all__”字段和相关字段
- javascript - HTML/Javascript 多次读取单个文件
- c# - 使用 SDK 17134 以 UWP 为目标的 NuGet 包中缺少 PackageReference