GROUP BY laravel 中的关系数据

GROUP BY the relationship data in laravel

我正在获取产品及其关系。我这样做了:

$data = $this->products->with('images')->with('colors')->where('slug', $slug)->first();

Product模型中,我写了:

public function images(){
  return $this->hasMany('App\Models\ProductImages', 'product_id');
}

public function colors(){
  return $this->hasMany('App\Models\ProductSizes', 'color_id');
}

我将 color_id 存储在 product_sizes table 中,所以现在当我存储 dd($data) 时。它在 object 中给了我 5 个数据,其中 size_id 不同但 color_id 相同。是否可以对颜色关系中的数据进行分组?

我尝试在 blade 中使用 array_unique,但这并没有让我使用以下功能:

public function colorInfo(){
    return $this->belongsTo('App\Models\Color', 'color_id');
}

我想对 colors 关系中的 color_id 进行分组,以显示产品的可用颜色。

代码按要求: 产品型号

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['name','slug','sku','category_id','brand_id','video','status', 'specification', 'description','warranty', 'vendor_id'];

public function getRules(){
    $rules = [
        'name' => 'bail|required|string|unique:products,name',
        'slug' => 'bail|required|string|unique:products,slug',
        'sku' => 'bail|required|string|unique:products,sku',
        'specification' => 'bail|required|string',
        'description' => 'bail|required|string',
        'category_id' => 'required|exists:product_categories,id',
        'brand_id' => 'nullable|exists:brands,id',
        'vendor_id' => 'nullable|exists:vendors,id',
        'video' => 'nullable|string',
        'warranty' => 'nullable|string',
        'status' => 'nullable|in:active,inactive',
    ];
    if($rules != 'add'){
        $rules['name'] = "required|string";
        $rules['slug'] = "required|string";
        $rules['sku'] = "required|string";
    }
    return $rules;
}
public function category(){
    return $this->belongsTo('App\Models\ProductCategory');
}

public function brand(){
    return $this->belongsTo('App\Models\Brand');
}

public function VendorName(){
    return $this->belongsTo('App\Models\Vendor', 'vendor_id');
}

public function images(){
  return $this->hasMany('App\Models\ProductImages', 'product_id');
}

public function sizes(){
  return $this->hasMany('App\Models\ProductSize', 'product_id');
}

public function colors(){
    return $this->hasMany('App\Models\ProductSize', 'product_id');
}

public function finalCategory(){
    return $this->belongsTo('App\Models\SecondaryCategory', 'category_id');
}

}

产品尺寸型号

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProductSize extends Model
{
    protected $fillable = ['product_id','size_id', 'selling_price', 'purchase_price', 'discount','stock', 'color_id', 'quantity','total_price'];

public function getRules(){
    $rules = [
        'product_id' => 'required|exists:products,id',
        'size_id' => 'required|exists:sizes,id',
        'color_id' => 'required|exists:colors,id',
        'selling_price' => 'required|string',
        'purchase_price' => 'required|string',
        'quantity' => 'required|string',
        'total_price' => 'required|string',
        'discount' => 'nullable|string',
        'stock' => 'required|string',
    ];
    return $rules;
}

public function colorInfo(){
    return $this->belongsTo('App\Models\Color', 'color_id');
}
}

试试这个

 $data = $this->products->with('images')->with('colors')->where('slug', $slug)->groupBy('product_sizes.color_id')->first();

先拿到产品。

$data = $this->products->with(['images', 'colors'])->where('slug', $slug)->first();

获得该产品的不同颜色。

$unique_product_colors = $data->colors->unique('color_id');

unique('color_id') 方法可以应用于集合实例以获取新集合,其中所有项目都将具有唯一 color_id