Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinct values with pluck

Tags:

I'm trying to retrieve data from database and bind them to a html select tag, and to bind them i need to use pluck so i get the field i want to show in a array(key => value), because of FORM::select. The normal pluck gets all the results, while i want to use distinct. My model is Room and it looks like:

    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'     ]; } 

While my function I'm using in the controller look like:

public function getRooms()     {         $roomType = Room::pluck('type','type');         $roomFloor = Room::pluck('floor','floor');          return view('roomgrid')->with('type',$roomType)->with('floor',$roomFloor);     } 

And my view contains this piece of code to get floors:

{{FORM::select('floor', $floor, null,['class'=>'basic'])}} 

Like this i get duplicated floors, that i don't want. Is there any way so i can get distinct floors and pluck them? Thanks in advance.

like image 385
Arlind Hajdari Avatar asked Feb 24 '17 20:02

Arlind Hajdari


People also ask

What's the difference between normal pluck and distinct pluck?

The normal pluck gets all the results, while i want to use distinct. My model is Room and it looks like: While my function I'm using in the controller look like: And my view contains this piece of code to get floors: Like this i get duplicated floors, that i don't want. Is there any way so i can get distinct floors and pluck them?

What is the difference between pluck () and where () function in eloquent?

While developing in eloquent, the column name can be passed as an argument in order to extract the values. Pluck () also accepts a second argument and if it happens to be an Eloquent collection, it will be another column name. To further strengthen the pluck () function, the wherein () function can be used.

What is the pluck () function in SQL?

The pluck () function is a multi-faceted command set which pulls out the most relevant information and yet works in tandem with other associated queries too. The pluck () function is a helpful function because it picks out the relevant information from a large set of data.

What is Laravel pluck ()?

Here in this article we are going to look into one such easy to use and yet a powerful query called the Laravel pluck (). How Does Laravel pluck () Work? The Laravel pluck () as the name suggests, extracts certain values, as suggested.


1 Answers

Why not use groupBy()?

$roomType = Room::groupBy('type')->pluck('type','type'); 
like image 82
Buglinjo Avatar answered Nov 10 '22 01:11

Buglinjo