Php Laravel,如何使用groupby显示产品标题及其相关项目
我很抱歉我写问题的方式,我不知道如何确切地解释我的问题,但希望你能得到我想要的;我想展示一个产品和它的附加组件,所有的工作都很好,但我想把这些附加组件放在它们的标题下,请看屏幕截图,你很清楚我的问题 所以我想停止重复类似的标题。。我对laravel很陌生:( 对于表格:我有:Php Laravel,如何使用groupby显示产品标题及其相关项目,php,laravel,Php,Laravel,我很抱歉我写问题的方式,我不知道如何确切地解释我的问题,但希望你能得到我想要的;我想展示一个产品和它的附加组件,所有的工作都很好,但我想把这些附加组件放在它们的标题下,请看屏幕截图,你很清楚我的问题 所以我想停止重复类似的标题。。我对laravel很陌生:( 对于表格:我有: class OrderCustomize extends Model { protected $table = "ordercustomizes"; protected $fillable = [ '
class OrderCustomize extends Model
{
protected $table = "ordercustomizes";
protected $fillable = [
'customizeproduct_id',
'userorder_id',
'product_id'
];
另一张表:
class CustomizeProduct extends Model
{
protected $table = "customizeproducts";
protected $fillable = [
'customizetitle_id',
'product_id',
'selection_type',
'selection_number',
'customize_title',
'customize_price'
];
下表:
class CustomizeTitle extends Model
{
protected $table = "customizetitles";
protected $fillable = [
'name'
];
在控制器中:
$customizeorders = OrderCustomize::where('userorder_id',$order_number)
->with('customizeproduct')
->get()
->groupBy('customizetitle_id');
在OrderCustomize模型中:
public function customizeproduct()
{
return $this->belongsTo(CustomizeProduct::class);
}
在CustomizeProduct模型中:
public function customizetitle()
{
return $this->belongsTo(CustomizeTitle::class);
}
在刀片中:
@if(count($customizeorders)>0)
@foreach ($customizeorders as $customizetitle => $groupCustomizes)
@foreach($groupCustomizes as $key=>$customize)
@if(($userorder->product_id)==($customize->product_id))
{{$customize->customizeproduct->customizetitle->name}}:
{{$customize->customizeproduct->customize_title}} .
@endif
@endforeach
@endforeach
@endif
转储数据:
Collection {#1554 ▼
#items: array:1 [▼
"" => Collection {#1553 ▼
#items: array:13 [▼
0 => OrderCustomize {#1485 ▶}
1 => OrderCustomize {#1486 ▼
#table: "ordercustomizes"
#fillable: array:3 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▼
"id" => 45
"customizeproduct_id" => 153
"userorder_id" => "36-ZA"
"product_id" => 2618
"created_at" => "2019-09-05 01:38:47"
"updated_at" => "2019-09-05 01:38:47"
]
#original: array:6 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
]
}
1:您可以使用下面的查询查询标题:
$customizeorders = OrderCustomize::select( DB::raw ("GROUP_CONCAT(DISTINCT customizetitle_id) as customizetitle_ids") )
->where('userorder_id',$order_number)
->with('customizeproduct')->get();
你可以只处理你得到的集合。例如,用逗号连接它。我需要一个真正的解决方案,这对我来说非常复杂,告诉我们你的数据库结构和你需要分组的数据转储。
dd($customizeorders)
As可能重复,我不知道其结构,因此请检查您的数据库设计并匹配上述代码。我希望您能帮助我更多,我需要对您的代码进行哪些更改才能使其与我一起工作?请与关系共享表。请帮助我,我在编辑我的问题后等待您回答,因为我没有我已经有很多时间生成行查询了