繁体   English   中英

Laravel groupBy 在没有 array_merge 的关系上

[英]Laravel groupBy on relationship without array_merge

我正在构建一个 Laravel 9 项目。 我创建了Product model 和ProductOption model。我的选项需要按名为region_code的列(并不总是设置)分组,以便可以轻松显示产品的选项。 我意识到如果我运行$product->options->groupBy('region_code')它会给我想要的结果,但我需要这些分组选项成为Product model 的一部分,因此我正在做一个array_merge

这种方法有效,但我觉得必须将其转换为数组并将结果合并回一个数组有点麻烦。

我确实尝试在我with子句中执行groupBy('region_code') ,但这并没有给我任何东西。

这是我当前的代码和 output

$selectedProduct = $request->input('product');
$selectedType = $request->input('type');

$product = Product::where('slug', $selectedProduct)
->where('is_enabled', true)
->with(['options' => function ($query) use ($selectedType) {
    $query->where('is_enabled', true)
          ->whereNotNull('region_code')
          ->where('slug', 'like', "%$selectedType%");
}])->first();

if (! $product) {
    return response()->json([
        'message' => "No product found."
    ], 404);
}

$product = array_merge($product->toArray(), [
    'options' => $product->options->groupBy('region_code')->toArray()
]);

return response()->json([
    'product' => $product
], 200);

在此处输入图像描述

一种更简洁的方法,我认为我可以覆盖options键,然后执行以下操作:

$options = $product->options->groupBy('region_code');
$product->options = $options;

return response()->json([
    'product' => $product
], 200);

但这会完全失去分组。

我该如何清理它?

更新

下面是每个 model 的表架构:

Product

Schema::create('products', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->string('title');
    $table->string('slug')->unique();
    $table->string('description')->nullable();
    $table->json('extra')->nullable();
    $table->boolean('is_enabled')->default(1)->index();
    $table->timestamps();
    $table->softDeletes();
});

ProductOption

Schema::create('product_options', function (Blueprint $table) {
    $table->uuid('id')->primary();
    $table->foreignUuid('product_id');
    $table->string('title');
    $table->string('slug')->unique();
    $table->string('description')->nullable();
    $table->string('region_code')->nullable()->index();
    $table->json('extra')->nullable();
    $table->string('stripe_id')->unique();
    $table->integer('quantity')->default(1);
    $table->boolean('is_enabled')->default(1)->index();
    $table->timestamps();
    $table->softDeletes();
});

在此处输入图像描述

$selectedProduct = $request->input('product');
$selectedType = $request->input('type');

$product = Product::where('slug', $selectedProduct)
    ->where('is_enabled', true)
    ->with(['options' => function ($query) use ($selectedType) {
        $query->where('is_enabled', true)
            ->whereNotNull('region_code')
            ->where('slug', 'like', "%$selectedType%")
            

             //just add here
            ->groupBy('region_code');


    }])->first();

if (!$product) {
    return response()->json([
        'message' => "No product found."
    ], 404);
}

return response()->json([
    'product' => $product
], 200);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM