Arl*_*ari 10 distinct laravel pluck laravelcollective
我正在尝试从数据库中检索数据并将它们绑定到html选择标记,并绑定它们我需要使用pluck所以我得到我希望在数组中显示的字段(key => value),因为FORM: :选择.正常的采摘获得所有结果,而我想使用不同的.我的模特是房间,它看起来像:
class Room extends Eloquent
{
public $timestamps = false;
protected $casts = [
'price' => 'float',
'floor' => 'int',
'size' => 'float'
];
protected $fillable = [
'capacity',
'description',
'price',
'floor',
'size',
'type',
'photo_name'
];
}
Run Code Online (Sandbox Code Playgroud)
虽然我在控制器中使用的功能看起来像:
public function getRooms()
{
$roomType = Room::pluck('type','type');
$roomFloor = Room::pluck('floor','floor');
return view('roomgrid')->with('type',$roomType)->with('floor',$roomFloor);
}
Run Code Online (Sandbox Code Playgroud)
我的视图包含这段代码来获取楼层:
{{FORM::select('floor', $floor, null,['class'=>'basic'])}}
Run Code Online (Sandbox Code Playgroud)
像这样我得到重复的地板,我不想要.有什么办法让我可以得到不同的地板并采摘它们吗?提前致谢.
Bug*_*njo 15
为什么不用groupBy()?
$roomType = Room::groupBy('type')->pluck('type','type');
Run Code Online (Sandbox Code Playgroud)
distinct可以解决问题:
Room::query()->distinct()->pluck('type');
Run Code Online (Sandbox Code Playgroud)
这将被翻译成以下 SQL:
SELECT DISTINCT type FROM rooms
Run Code Online (Sandbox Code Playgroud)
你可以这样做
$roomType = Room::pluck('type')->unique();
$roomFloor = Room::pluck('floor')->unique();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14264 次 |
| 最近记录: |