Php 将不同的column1值分组并显示在Laravel搜索结果中相同的column2值下
我正在使用Laravel 5.2开发一个电子商务系统,该系统具有用于分类的自定义字段 关系和表格如下所示 产品表Php 将不同的column1值分组并显示在Laravel搜索结果中相同的column2值下,php,laravel,laravel-5.2,laravel-blade,Php,Laravel,Laravel 5.2,Laravel Blade,我正在使用Laravel 5.2开发一个电子商务系统,该系统具有用于分类的自定义字段 关系和表格如下所示 产品表 列-ProductID、ProductName 我有两款产品(HP和联想笔记本电脑),其数据显示在CustomFieldValues表中 自定义字段(如品牌、RAM、处理器、存储大小)存储在CustomFields表中 自定义字段和类别之间的关系是,每个类别都有不同的自定义字段 例如,汽车可能有颜色和传输自定义字段。 我只为这个问题选择了笔记本电脑类别。这就是CategoryID=1
列-ProductID、ProductName 我有两款产品(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
@extends('layouts.default'))
@节(“内容”)
@foreach($子类别作为$子类别)
{{$subcategory->CustomFieldName}-{{$subcategory->CustomFieldValue}
@endforeach
@端部
结果是这样的
笔记本电脑品牌-惠普处理器-核心i3
RAM-4GB
存储大小-500GB
笔记本电脑品牌-联想
处理器-核心i3
RAM-8GB
存储大小-1 TB
现在,我想知道的是如何组织和显示数据,如 笔记本电脑品牌 HP
联想
内存 4GB
8GB
处理器 核心i5
存储大小 1TB
500GB
我可以使用现有的结果集获得所需的结果,但要通过数组循环和重新排列,还是必须处理控制器的查询。您必须使用
groupBy
和orderBy
。在文件中:
雄辩地
$results = App\ModelName::orderBy('CustomFieldName', 'asc')
->groupBy('CustomFieldName')
->get();
在我的例子中,
groupBy
非常适合我显示类似类型的数据。您可以尝试以下操作。您可以这样尝试以获得结果:
$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”列以将输出显示到视图中。您可以尝试此操作
这是主表(自定义字段)
这是子表(CustomFieldValues)
现在必须指定两个表的外键关系
CustomFields.CustomFieldID = CustomFieldValues.CustomFieldID
Laravel提供了雄辩的关系
在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);
}
渲染视图
@extends('layouts.default')
@section('content')
<div class="row">
@foreach($subcategories->fieldValue as $subcategory)
<p>
{{$subcategory->CustomFieldName}} - {{$subcategory->CustomFieldValue}}
<p>
@endforeach
</div>
@endsection
@extends('layouts.default'))
@节(“内容”)
@foreach($subcategories->fieldValue作为$subcategory)
{{$subcategory->CustomFieldName}-{{$subcategory->CustomFieldValue}
@endforeach
@端部
现在您可以达到您的要求了。您尝试了什么?你的密码在哪里?请访问帮助中心并阅读。抱歉,在查看您的答案之前,我更新了问题。您的答案实际上非常有用,尤其是查询部分。我能通过一个问题得到所需的结果。查询返回4行,其中自定义字段值用逗号分隔。我能够使用foreach/explode循环遍历它们。问题是有一个字段值数据包含core i3、core i3(两次),而其他字段值数据是HP、Lenovo。。。我怎样才能在该列中得到不同的结果。这意味着对于特定记录只有一个core i3。不用担心,我只从CustomFieldValue中选择了不同的结果。即-我将DB::raw(“GROUP_CONCAT(CustomFieldValue)作为fieldValue”)更改为DB::raw(“GROUP_CONCAT(DISTINCT(CustomFieldValue))作为fieldValue”)
@extends('layouts.default')
@section('content')
<div class="row">
@foreach($subcategories->fieldValue as $subcategory)
<p>
{{$subcategory->CustomFieldName}} - {{$subcategory->CustomFieldValue}}
<p>
@endforeach
</div>
@endsection