簡體   English   中英

在 Laravel 搜索結果的相同 column2 值下分組並顯示不同的 column1 值

[英]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”列以將輸出顯示到您的視圖中。

您必須將groupByorderBy一起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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM