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.
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?
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.
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.
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.
Why not use groupBy()
?
$roomType = Room::groupBy('type')->pluck('type','type');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With