Laravel查询存在几个
Laravel Query Existence of several
我阅读了有关查询构建器的 Laravel 文档,但我无法理解:
现状:
我的用户模型有很多 upperCategories,我的 upperCategory 模型有很多子类别。这就是为什么我可以这样做:auth()->user()->upperCategories()->with('subCategories')->get()->toArray();
请注意,我使用预先加载来提高性能。这段代码工作正常。它 returns 类似这样的东西:
[
"id" => 1,
"user_id" => 2,
"category" => "Project",
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
],
]
我想要的:
我的子类别模型有很多微类别。我想检查每个 SubCategory 是否存在 microCategory 所以最后我可以创建类似的东西:
[
"id" => 1,
"user_id" => 2,
"category" => "Project",
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
"micro_category_exists" => true,
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
"micro_category_exists" => false,
],
]
我不想要的是为我拥有的每个子类别发送一个sql查询。我知道我可以使用 exists() 方法解决这样的问题,但是我想在一个查询中完成所有事情。这可能吗?
由于您想向 subCategories 关系添加一个额外的字段,因此您需要向 with
函数添加一个闭包
auth()->user()
->upperCategories()
->with([
'subCategories' => function ($query) {
$query->withCount('microCategories as micro_category_exists');
}
])
->get()
->toArray();
这个简单的查询将加载 microCategories COUNT,给你这样的东西:
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
"micro_category_exists" => 5,
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
"micro_category_exists" => 0,
],
]
您可以将这些数字用作 truthy/falsy 值(0
可以转换为 php 中的 false
)。
如果您确实需要查询中的真值,则需要编写一些自定义逻辑。在 laravel 的最新版本中,withAggregate
函数的存在使这变得非常简单。
auth()->user()
->upperCategories()
->with([
'subCategories' => function ($query) {
$query->withAggregate('microCategories as micro_category_exists', 'count(*) > 0');
}
])
->get()
->toArray();
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
"micro_category_exists" => true,
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
"micro_category_exists" => false,
],
]
我阅读了有关查询构建器的 Laravel 文档,但我无法理解:
现状:
我的用户模型有很多 upperCategories,我的 upperCategory 模型有很多子类别。这就是为什么我可以这样做:auth()->user()->upperCategories()->with('subCategories')->get()->toArray();
请注意,我使用预先加载来提高性能。这段代码工作正常。它 returns 类似这样的东西:
[
"id" => 1,
"user_id" => 2,
"category" => "Project",
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
],
]
我想要的: 我的子类别模型有很多微类别。我想检查每个 SubCategory 是否存在 microCategory 所以最后我可以创建类似的东西:
[
"id" => 1,
"user_id" => 2,
"category" => "Project",
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
"micro_category_exists" => true,
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
"micro_category_exists" => false,
],
]
我不想要的是为我拥有的每个子类别发送一个sql查询。我知道我可以使用 exists() 方法解决这样的问题,但是我想在一个查询中完成所有事情。这可能吗?
由于您想向 subCategories 关系添加一个额外的字段,因此您需要向 with
函数添加一个闭包
auth()->user()
->upperCategories()
->with([
'subCategories' => function ($query) {
$query->withCount('microCategories as micro_category_exists');
}
])
->get()
->toArray();
这个简单的查询将加载 microCategories COUNT,给你这样的东西:
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
"micro_category_exists" => 5,
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
"micro_category_exists" => 0,
],
]
您可以将这些数字用作 truthy/falsy 值(0
可以转换为 php 中的 false
)。
如果您确实需要查询中的真值,则需要编写一些自定义逻辑。在 laravel 的最新版本中,withAggregate
函数的存在使这变得非常简单。
auth()->user()
->upperCategories()
->with([
'subCategories' => function ($query) {
$query->withAggregate('microCategories as micro_category_exists', 'count(*) > 0');
}
])
->get()
->toArray();
"sub_categories" => [
[
"id" => 1,
"upper_category_id" => 1,
"category" => "myFristProject",
"micro_category_exists" => true,
],
[
"id" => 4,
"upper_category_id" => 1,
"category" => "mySecondProject",
"micro_category_exists" => false,
],
]