Php Laravel resource groupby结果
我有提供结果的资源API,但我想根据关系模型对它们进行分组 代码Php Laravel resource groupby结果,php,laravel,Php,Laravel,我有提供结果的资源API,但我想根据关系模型对它们进行分组 代码 控制器 $outlet = Outlet::where('slug', $slug)->with(['barcodes.product', 'damages', 'images'])->first(); 结果 { "data": { "id": 1, "name": "Outlet One", "slug": "outlet-one", "add
控制器
$outlet = Outlet::where('slug', $slug)->with(['barcodes.product', 'damages', 'images'])->first();
结果
{
"data": {
"id": 1,
"name": "Outlet One",
"slug": "outlet-one",
"address": "Jl Raya Bogor No.200",
"cover": null,
"phone": "0211111112",
"products": [
{
"id": 1,
"sku": "AB001SU",
"serial_number": 5245412185,
"price": 120000,
"discount": null,
"product": {
"id": 1,
"name": "Product One",
"slug": "product-one",
"stock": "70",
"cover": null,
"description": "This is first product description.",
"sku": "AB001SU",
"price": 120000,
"discount": null
}
},
{
"id": 2,
"sku": "FD51",
"serial_number": 778516,
"price": 75300,
"discount": 5300,
"product": {
"id": 1,
"name": "Product One",
"slug": "product-one",
"stock": "70",
"cover": null,
"description": "This is first product description.",
"sku": "AB001SU",
"price": 120000,
"discount": null
}
},
{
"id": 3,
"sku": "7609FS",
"serial_number": 232547544,
"price": 35900,
"discount": null,
"product": {
"id": 2,
"name": "Product Two",
"slug": "product-two",
"stock": "120",
"cover": null,
"description": "This is second product description.",
"sku": "FRY8016",
"price": 450000,
"discount": 50000
}
}
]
},
"message": "Outlet retrieved successfully."
}
正如您在产品中所看到的:“[…]
id1和2
属于产品一
我是否可以根据条形码对产品进行分组。产品基于产品
型号
更新
更清楚地说,我要找的是这样的东西:
{
"data": {
"id": 1,
"name": "Outlet One",
"slug": "outlet-one",
"address": "Jl Raya Bogor No.200",
"cover": null,
"phone": "0211111112",
"products": [
{
"id": 1,
"name": "Product One",
"slug": "product-one",
"stock": "70",
"cover": null,
"description": "This is first product description.",
"sku": "AB001SU",
"price": 120000,
"discount": null
"barcodes": { // now barcodes are grouped by prodcuts
"id": 1,
"sku": "AB001SU",
"serial_number": 5245412185,
"price": 120000,
"discount": null,
},
{
"id": 2,
"sku": "FD51",
"serial_number": 778516,
"price": 75300,
"discount": 5300,
},
},
{
"id": 2,
"name": "Product Two",
"slug": "product-two",
"stock": "120",
"cover": null,
"description": "This is second product description.",
"sku": "FRY8016",
"price": 450000,
"discount": 50000
"barcodes": { // now barcodes are grouped by prodcuts
"id": 3,
"sku": "7609FS",
"serial_number": 232547544,
"price": 35900,
"discount": null,
}
}
]
},
"message": "Outlet retrieved successfully."
}
更新2
插座型号
public function barcodes()
{
return $this->belongsToMany(Barcode::class, 'outlet_products');
}
public function outlets()
{
return $this->belongsToMany(Outlet::class, 'outlet_products');
}
public function product()
{
return $this->belongsTo(Product::class);
}
条形码型号
public function barcodes()
{
return $this->belongsToMany(Barcode::class, 'outlet_products');
}
public function outlets()
{
return $this->belongsToMany(Outlet::class, 'outlet_products');
}
public function product()
{
return $this->belongsTo(Product::class);
}
从未尝试过类似的方法,但您可能可以在您的Outlet
型号中进行此操作
使用App\Http\Resources\ProductsResource;
公共函数getProductsAttribute()
{
$barcodes=$this->barcodes
->loadMissing('产品')
->makeHidden(“产品”);
$products=$barcodes->PULL('product')->keyBy('id');
$groupedBarcodes=$barcodes->groupBy(函数($barcode){
返回$barcode->product->id;
});
返回产品资源::集合($products->map(函数($product,$id)使用($groupedBarcodes){
返回$product->setAttribute('barcode',$groupedbarcode[$id]);
}))->解决();
}
你的资源输出是什么?在你的出口
模型中写下这个
public function products(){
return $this->hasMany(Product::class, 'product_id','id');
}
public function barcodes(){
return $this->hasMany(Barcode::class,'barcode_id','id');
}
$outlet = Outlet::where('slug', $slug)->products()->barcodes()->with(['your_field'])->first();
在您的产品
模型中编写以下内容
public function products(){
return $this->hasMany(Product::class, 'product_id','id');
}
public function barcodes(){
return $this->hasMany(Barcode::class,'barcode_id','id');
}
$outlet = Outlet::where('slug', $slug)->products()->barcodes()->with(['your_field'])->first();
那么,对于您的查询,只需编写以下内容
public function products(){
return $this->hasMany(Product::class, 'product_id','id');
}
public function barcodes(){
return $this->hasMany(Barcode::class,'barcode_id','id');
}
$outlet = Outlet::where('slug', $slug)->products()->barcodes()->with(['your_field'])->first();
您的预期输出是什么?@Shizzen83现在我的条形码数据是大写的,并且每个数据下都有产品详细信息?现在我想根据产品对我的条形码进行分组,条形码位于产品下,因此产品一将有2个条形码,而产品二将有一个条形码barcode@Shizzen83更新了我的问题,对不起,我不能再多提问题了清楚:请分享您的门店型号,我想我可以帮助您。好的,我应该如何处理它?我应该调用我的控制器来获取getProductsAttribute
?它是自动调用的,这要感谢appends
属性,它说参数1传递给illumb\\Database\\elount\\Collection::map()第47行的C:\\laragon\\www\\ims web\\app\\Outlet.php中必须是可调用的、给定字符串的、调用的
第47行是$products=$barcodes->map('product')->keyBy('id'))
编辑,将映射
替换为提取
此返回数据,但它要求我避免使用我的资源api。当前我的代码如下:'data'=>new OutletsResource($outlet),
但如果我想使用您的模型代码(工作正常)我应该使用'data'=>$outlet,
这意味着我会有很多对我没有用处的额外数据。你有什么想法吗?谢谢,但我的outlet
表welcome@mafortis中没有产品id
列,我知道调用未定义的方法illumb\\Database\\elount\\Builder::products()首先执行此命令composer dump autoload
,然后在tinker
中尝试此命令。然后在生产中尝试。