I want to create a select box like the one below using illuminate\\html :
Sorry for the late reply
Obviously lists
method has been deprecated
in Laravel, but you can use the pluck method.
For Eg:
Laravel 5.7
public function create()
{
$countries = Country::pluck('country_name','id');
return View::make('test.new')->with('countries', $countries);
}
and in the view if you are FORM
components just pass as
{{ Form::select('testname',$countries,null,['class' => 'required form-control select2','id'=>'testname']) }}
if will generate the dropdown
but i have a situation to show that select country as the first option and as null value
<option value="" selected="selected">--Select Country--</option>
so I have referred to
https://stackoverflow.com/a/51324218/8487424
and fixed this, but in later times if I want to change this I hate being changing it in the view
So have created the helper function based on https://stackoverflow.com/a/51324218/8487424
and placed in the Country Model
public static function toDropDown($tableName='',$nameField='',$idField='',$defaultNullText='--Select--')
{
if ($idField == null)
{
$idField="id";
}
$listFiledValues = DB::table($tableName)->select($idField,$nameField)->get();
$selectArray=[];
$selectArray[null] = $defaultNullText;
foreach ($listFiledValues as $listFiledValue)
{
$selectArray[$listFiledValue->$idField] = $listFiledValue->$nameField;
}
return $selectArray;
}
and in controller
public function create()
{
$countries = Country::toDropDown('countries','name','id','--Select Country--');
return View::make('test.new')->with('countries', $countries);
}
and finally in the view
{{ Form::select('testname',$countries,null,['class' => 'required form-control select2','id'=>'testname']) }}
and the result is as expected, but I strongly recommend to use pluck()
method
I have added toArray()
after pluck
$items = Item::get()->pluck('name', 'id')->toArray();
{{ Form::select('item_id', [null=>'Please Select'] + $items) }}
For Laravel 5 :
$items = Items::lists('name', 'id');
Push an item onto the beginning of the collection.
$items->prepend($value, $key = null);
I was trying to do the same thing in Laravel 5.8 and got an error about calling pluck statically. For my solution I used the following. The collection clearly was called todoStatuses.
<div class="row mb-2">
<label for="status" class="mr-2">Status:</label>
{{ Form::select('status',
$todoStatuses->pluck('status', 'id'),
null,
['placeholder' => 'Status']) }}
</div>
To populate the drop-down select box in laravel we have to follow the below steps.
From controller we have to get the value like this:
public function addCustomerLoyaltyCardDetails(){
$loyalityCardMaster = DB::table('loyality_cards')->pluck('loyality_card_id', 'loyalityCardNumber');
return view('admin.AddCustomerLoyaltyCardScreen')->with('loyalityCardMaster',$loyalityCardMaster);
}
And the same we can display in view:
<select class="form-control" id="loyalityCardNumber" name="loyalityCardNumber" >
@foreach ($loyalityCardMaster as $id => $name)
<option value="{{$name}}">{{$id}}</option>
@endforeach
</select>
This key value in drop down you can use as per your requirement. Hope it may help someone.
Laravel 5.*
In your controller:
$items= Items::pluck('name', 'id')->toArray();
return view('your view', compact('items', $items));
In your view:
{{ Form::select('organization_id', $items, null, []) }}