[英]Group and Display different column1 values under the same column2 values from Laravel Search Results
我正在使用 Laravel 5.2 開發電子商務系統,該系統具有類別的自定義字段。
關系和表格如下。
產品表
列 - 產品 ID、產品名稱
我有兩個產品(HP 和聯想筆記本電腦),其數據顯示在CustomFieldValues表中。
品牌、RAM、處理器、存儲大小等自定義字段存儲在CustomFields表中。
自定義字段和類別之間的關系是我們為每個類別有不同的自定義字段。
例如,汽車可能具有顏色和傳輸自定義字段。 我只為這個問題選擇了筆記本電腦類別。 這就是為什么 CategoryID = 1
每個產品的自定義值存儲在CustomFieldValues表中
這是控制器
public function subcategories($CategoryID)
{
$subcategories = DB::table('customfields')
->leftJoin('customfieldvalues', 'customfields.CustomFieldID', '=', 'customfieldvalues.CustomFieldID')
->select('customfieldvalues.*', 'customfields.CustomFieldName')
->groupBy('ProductID', 'customfields.CustomFieldID')
->where('customfields.CategoryID', '=', $CategoryID)
->get();
return view('categories.subcategories')
->with('subcategories', $subcategories);
}
這是我的看法
@extends('layouts.default')
@section('content')
<div class="row">
@foreach($subcategories as $subcategory)
<p>
{{$subcategory->CustomFieldName}} - {{$subcategory->CustomFieldValue}}
<p>
@endforeach
</div>
@endsection
結果是這樣
筆記本電腦品牌 - 惠普
處理器 - 酷睿 i3
內存 - 4GB
存儲大小 - 500GB
筆記本電腦品牌 - 聯想
處理器 - 酷睿 i3
內存 - 8GB
存儲大小 - 1 TB
現在,我想知道的是如何組織和顯示數據,如 as
生命值
聯想
4GB
8GB
酷睿i5
1TB
500GB
我可以使用現有的結果集獲得所需的結果,而是通過遍歷數組並重新排列,或者我是否必須處理控制器的查詢。
你可以嘗試這樣得到結果:
$data = DB::table('tbl_name')
->select('CustomFieldName', DB::raw('GROUP_CONCAT(CustomFieldValue) as fieldValue'))
->groupBy('CustomFieldName')
->get();
結果輸出為:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[CustomFieldName] => Laptop Brands
[fieldValue] => HP,Lenovo
)
[1] => stdClass Object
(
[CustomFieldName] => Processor
[fieldValue] => Core i5
)
[2] => stdClass Object
(
[CustomFieldName] => RAM
[fieldValue] => 4GB,8GB
)
[3] => stdClass Object
(
[CustomFieldName] => Storage Size
[fieldValue] => 1TB
)
)
)
因此,現在您必須探索“fieldValue”列以將輸出顯示到您的視圖中。
您必須將groupBy
與orderBy
一起orderBy
。 在文檔中: https : //laravel.com/docs/5.5/queries#ordering-grouping-limit-and-offset
$results = DB::table('table_name')
->orderBy('CustomFieldName', 'asc')
->groupBy('CustomFieldName')
->get();
在雄辯中,
$results = App\ModelName::orderBy('CustomFieldName', 'asc')
->groupBy('CustomFieldName')
->get();
在我的情況下, groupBy
對我來說很好地顯示了類似類型的數據。 你可以試試這個..
你可以試試這個。
這是主表(CustomFields)
這是子表 (CustomFieldValues)
現在您必須指定與兩個表的外鍵關系,
CustomFields.CustomFieldID = CustomFieldValues.CustomFieldID
Laravel 提供 Eloquent 關系,https: //laravel.com/docs/5.2/eloquent-relationships#one-to-many
在 App\\CustomFields 模型中編寫此函數
public function fieldValue()
{
return $this->hasMany('App\CustomFieldValues');
}
在控制器函數中編寫此代碼,
public function subcategories($CategoryID)
{
$subcategories = App\CustomFields::with('fieldValue')->get();
return view('categories.subcategories')->with('subcategories', $subcategories);
}
https://laravel.com/docs/5.2/eloquent-relationships#constraining-eager-loads
渲染視圖,
@extends('layouts.default')
@section('content')
<div class="row">
@foreach($subcategories->fieldValue as $subcategory)
<p>
{{$subcategory->CustomFieldName}} - {{$subcategory->CustomFieldValue}}
<p>
@endforeach
</div>
@endsection
現在你可以達到你的要求了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.