I´m trying to create a form select in laravel.
I was created perfectly, but in my view first option is 0 and one first option empty
I need to remove this 0 and option empty, but i don't know how i can do it, this is my actual code:
<div class="form-group row ">
{!! Form::label('restaurant_id', trans("lang.blog_restaurant_id"),['class' => 'col-3 control-label text-right']) !!}
<div class="col-9">
{!! Form::select('restaurant_id', [null => null, $restaurant], null, ['class' => 'select2 form-control']) !!}
<div class="form-text text-muted">{{ trans("lang.blog_restaurant_id_help") }}</div>
</div>
</div>
$restaurant it's one data array for to fill my select. I read that with first option null put selected option empty, but generate a empty option and 0.
Thanks for help me.
UPDATE
Laravel include optgroup into Form::select that it´s triggering my 0 this it´s the problem. i need delete optgroup
<select class="select2 form-control select2-hidden-accessible" id="restaurant_id" name="restaurant_id" tabindex="-1" aria-hidden="true">
<option value="" selected="selected"></option>
<optgroup label="0">
<option value="17">Foody Lindgren, Cremin and Erdman gdfgfg fgfg fg fgfg g fggf</option>
<option value="28">restaurante de pruebas</option>
<option value="29">probando</option>
<option value="30">pincho de castilla2</option>
</optgroup>
update 2
getting list of restaurant:
$restaurant = $this->restaurantRepository->pluck('name', 'id');
this is result in blade:
{"17":"Foody Lindgren, Cremin and Erdman gdfgfg fgfg fg fgfg g fggf","28":"restaurante de pruebas","29":"probando","30":"pincho de castilla2"}
Add toArray():
$restaurant = $this->restaurantRepository->pluck('name', 'id');
$restaurant->toArray();
same result
Please try this. Add blank key and blank value like below.
{!! Form::select('restaurant_id', ["" => "", $restaurant], null, ['class' => 'select2 form-control']) !!}
It seems that the $restaurant is an array.
So, you'd better do something like this:
Form::select('restaurant_id', $restaurant, null, ['class' => 'select2 form-control'])
you may need a proper mapping for your $restaurant in your controller, depending on what you need, like adding an empty option in the beginning.
// in Controller
$options = ["" => "select"] + $restaurant;
// In view
Form::select('restaurant_id', $options, null, ['class' => 'select2 form-control'])
update 2
You should make sure to send the $restaurant as an array:
$restaurant = $this->restaurantRepository->pluck('name', 'id')->toArray();
For adding an empty option:
$restaurant = ["" => "select"] + $restaurant;
There is also another option:
You can even add the select box manually instead of using Form::select.
<select class="select2 form-control" id="restaurant_id" name="restaurant_id">
<option value="" selected="selected"></option>
#foreach($restaurant as $value => $option)
<option value="{{ $value }}">{{ $option }}</option>
#endforeach
</select>
Related
I'm working on validating my registration form.
If the input is a regular input, i can populate the previous value if it fails like so
<input type="tel" name="phone" value="{{old('phone')}}">
How do I populate the value on a select option?
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
I suggest putting all you ages in an array and pass that array to view:
$ages = ['18-25','25-50','50-75','75+']; // More dynamic and you can extend in anytime
Now changes in html :
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
// 1. first change which is creating your problem
<option {{ old('age_range') ? "" : "selected" }} disabled>Select your age range</option>
// 2. This is just optimization for short code
#foreach($ages as $age)
<option {{old('age_range') ==$age" ? $selected : ""}} value="{{$age}}">{{$age}}</option>
#endforeach
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
So in point 1 you are always applying the selected with first option.
Suppose your old value was 46-55
your html looks like :
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option selected value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
if you take a look at above html there are 2 selected options. Html always picks the first one this is what creating the problem.
Point 1. will check if there is a old value available it will not apply the selected to the first placeholder option.
{!! Form::select('name', $varableyouwanttodisplay, old('name'),
['id'=>'id', 'class' => 'form-control select2', 'data-
placeholder' => 'select','required', 'style' => 'width:100%']) !!}
try like above code.
#php $selected = old('experience') ?: 66; #endphp
and than
{{ Form::select('experience', $experience, $selected, ['class' => 'form-control']) }}
This works in a case when a blade template loads for the first time and doesn't have 'old' value and you need predefined selected option(I.e. 66) for that case.
I want to fetch drop down option value & name from database, to do that my controller code is
$roles = Role::pluck('role','user_id');
return view('users.add_role',compact('roles'));
to fetch this data from drop down list my view file code is
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role->user_id }} "> {{ $role->role }} </option>
#endforeach
</select>
but it says error
Trying to get property of non-object (View: C:\xampp\htdocs\auth2\resources\views\users\add_role.blade.php)
if i just only use
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role }} "> {{ $role }} </option>
#endforeach
</select>
then it shows role column of the table, but it not pass option value to database. so what is the correct way to generate option value & name from database?
Your $roles variable containt an array which is looks like
[0] => 'your_role'
[1] => 'your_role'
Where is 0 and 1 index is user_id. So your user_id is set as an index of your $roles array. Simply do it dd($roles) and check the output. $key as $role will work. Where $key will containt the user_id.
#foreach($roles as $key => $role)
<option value="{{ $key }} "> {{ $role }} </option>
#endforeach
I use laravel collective package to generate dropdown list much more easier,
you can do things like, on your blade template and it will render the list for you
{{ Form::select('role', $role )) }}
A database table has two fields first_name and last_name.
If I want to fill a dropdown box i can use this code:
{!! Form::select('driver_id', App\Driver::pluck('first_name','id'), null, ['class' => 'form-control']) !!}
But if I want concat two fileds i should use a SelectRow on my model:
{!! Form::select('driver_id', App\Driver::select(DB::raw("CONCAT(first_name,' ', last_name) AS full_name, id"))->pluck('full_name','id'), null, ['class' => 'form-control']) !!}
But this solution does not works and this is what i get:
<select class="form-control" name="driver_id">
<option value="">Autista...</option>
<option value="11"> </option>
<option value="12"> </option>
<option value="13"> </option>
.....
</select>
That is.... the select does not get "full_name" filed but get the "id" field.
How to solve ?
I think you can try this:
{{ Form::select('driver_id', App\Driver::select(DB::raw("CONCAT(first_name,' ', last_name) AS full_name, id"))->lists('full_name','id'), null, ['class' => 'form-control']) }}
Hope this work for you!
Try This in your blade file:
App\Driver::select(
DB::raw("CONCAT(first_name,' ',last_name) AS full_name"),'id')
->pluck('full_name', 'id');
SOLUTION
The problem is the underscore character in the alias field name "full_name".
This code works fine:
App\Driver::selectRaw("CONCAT(first_name,' ',last_name) as fullname ,id")->pluck("fullname", "id")
Thanks everyone.
I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}
I'm fairly new to Symfony2 and am working with the form builder trying to create a dropdown menu from the database. I can populate the dropdown without problem but each options value attribute just gets set to a number when it needs to be set to either the options text or no value attribute but I can't find anything in the documentation about setting the value.
$builder->add('institution', 'entity', array(
enter code here 'class' => 'JacksonFramesStoreBundle:Institution',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('i')
->where('i.type = :type')
->setParameter('type', 'University');
},
'empty_value' => 'Select:',
'empty_data' => null,
));
This outputs:
<select id="selectUniversity" name="selectUniversity">
<option value="0"></option>
<option value="1">Australian Catholic University</option>
<option value="2">Australian National University</option>
<option value="3">Bond University</option>
</select>
EDIT - this is the relevant section of the twig
<!-- University Panel -->
<div id="uniPanel" style="display:none;">
<p>{{ form_label(form.institution) }}<br />
<span class="inputLine">
{{ form_errors(form.institution) }}
{{ form_widget(form.institution) }}
</span>
</p>
</div>
In the controller, after if ($form->isValid()), try using:
$value = ($entity->getInstitution()) ? $entity->getInstitution()->getId() : null);
I don't really know if I really understood your question