I want the select dropdown value to be selected in my edit form.
In my controller
public function edit($id)
{
$vedit = DB::table('vehicles')->where('id', $id)->first();
$cartype= DB::table('car_category')->pluck('cartype');
return view('vehicles.edit', compact('vedit','cartype'));
}
In view
{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
#foreach($cartype as $cartypes)
<option value="{{ $cartypes}}">{{ $cartypes}}</option>
#endforeach
</select>
How can I achieve this?
You can add selected attribute if it's the choosen one like this :
{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
#foreach($cartype as $cartypes)
<option value="{{ $cartypes}}" {{ $cartypes == $vedit->vehicle_type ? 'selected' : ''}}>{{ $cartypes}}</option>
#endforeach
</select>
What version of Laravel are you using ? Looks like you're using Laravel Collective Form facade.
In that case, this should work fine:
{!! Form::label('Vehicle Type', 'Vehicle Type') !!}
{!! Form::select('vehicle_type', $cartype, $vedit->vehicle_type ?: old('vehicle_type), ['class' => 'form-control']) !!}
Assuming $vedit->vehicle_type is your previously stored vehicle type. So it will pre-select that when editing. In case of creating new, old('vehicle_type') should persist the previously selected value on failed validations.
By calling pluck() an array of values is already returned for the car types.
So just use it like this natively with Laravel Collective:
{!! Form::label('Vehicle Type', 'Vehicle Type') !!}
{!! Form::select('vehicle_type', $cartype, null, ['class' => 'form-control']) !!}
Note, I have also changed your double curly braces. The double curly braces escape the output - given the Form facade returns HTML code, you want this to be unescaped.
More info on generating drop down lists with Laravel Collective; https://laravelcollective.com/docs/5.4/html#drop-down-lists
Edit show currently selected value and old value if form fails validation:
{!! Form::label('Vehicle Type', 'Vehicle Type') !!}
{!! Form::select('vehicle_type', $cartype, old('vehicle_type', $vedit->vehicle_type), ['class' => 'form-control']) !!}
Related
am trying to make old values which would be used when am editing but it's returning
htmlspecialchars(): Argument #1 ($string) must be of type string, array giveneverytime
but when i dd the variable i found that it has array but when it reaches the option it changes into error
this is my select in blade
{!! Form::label('functional_area_id', 'Functional Area', ['class' => 'bold']) !!}
<?php
$functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds);
?>
{{-- this codes works perfect but it doesn't display the relation between child and parent and i want that to be displayed
{!! Form::select('functional_area_id[]', $functionalAreas, $functionalAreaIds, array('class'=>'form-control select2-multiple', 'id'=>'functional_area_id','multiple'=>'multiple')) !!} --}}
<select name="functional_area_id[]" id="functional_area_id" class="form-control select2-multiple" multiple="multiple">
#foreach ($menulist as $category)
<option value={{ $category->functional_area_id }} {{ $functionalAreaIds }} >{{ $category->functional_area }}</option>
#if (count($category->children) > 0)
#include('submenu', ['submenu' => $category->children, 'parent' =>
$category->functional_area])
#endif
#endforeach
</select>
{!! APFrmErrHelp::showErrors($errors, 'functional_area_id') !!}
</div>
this is the controller which fetches the array
public function jobFunctionals()
{
return $this->hasMany('App\JobFunctionalAreas', 'job_id', 'id');
}
public function getJobFunctionalsArray()
{
return $this->jobFunctionals->pluck('functional_area_id')->toArray();
}
You can only output strings using {{ $variable }}, because Blade will run $variable through htmlspecialchars escaping, that's why you get an error trying to output {{ $functionalAreaIds }} which, judging by the name, is an array.
I have no idea what you are including with the submenu partial, but it is likely invalid HTML. The only tags that are allowed within a select element are option and optgroup tags. Optgroups need to be opened and closed around options, so I don't see how this would work in your setup.
Also, you have syntax errors in your option tag:
<option value={{ $category->functional_area_id }} {{ $functionalAreaIds }} >{{ $category->functional_area }}</option>
What do you expect this to do? This would - if it worked at all - result in something like this:
<option value=1 [1,5,6] >Area 1</option>
Which is clearly not valid HTML. What do you expect the array to do after every option value?
To fix the option part of your code, you can do something like this:
<option value="{{ $category->functional_area_id }}" #if(in_array(category->functional_area_id, $functionalAreaIds)) selected #endif >{{ $category->functional_area }}</option>
This will set all options selected that are within the $functionalAreaIds array.
Maybe you should get rid of the code including the submenu when testing, because this is likely to break your HTML too, and then take it from there.
Another tip:
<?php
$functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds);
?>
can be written in blade like this:
#php($functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds))
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>
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 want to get the id of the selected value or item from a dropdown selectbox because I want to save it in the database. So, I tried passing the it as a value to get it with an hidden field on the page but it does work. How do I do this?
This is the form page:
<p>{!! Form::select('companyname', array('' => 'Select a Company') + $listCompanies) !!} </p>
#foreach($istCompaniesId as $company)
#if(companyname->text === $listCompaniesId->value)
{!! Form::hidden('company_id', $listCompaniesId->value) !!}
#endif
#endforeach
This is the controller:
$listCompanies = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'companyname')->toArray();
$listCompaniesId = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'id')->toArray();
return view('product.create')
->with('listCompanies', $listCompanies)
->with('listCompaniesId', $listCompaniesId);
Since you are using Laravel 5.1, I suggest not to use the form helper. You can simply combine the option tag with blade's #foreach.
<select name="company_id">
<option value="">Choose Company</option>
#foreach($listCompanies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
#endforeach
</select>
How to add "selected" on the dropdown option in Laravel?
I am doing editing profile. In the profile I have a dropdown list, the options are pulled from database, like this.
//Controller
$gender= Gender::lists('gender', 'id');
//View
{{ Form::select('gender', $gender, null, array('class' => 'form-control')) }}
//Output
<select class="form-control" name="gender"><option value="1">Male</option><option value="2">Female</option></select>
My question: If the user is female, how to add "selected" in the female option if the user has saved female in database before, so that I can show the gender when the page is loaded?
You can set the default value at the 3rd parameter.
Assume Female id is 2
{{ Form::select('gender', $gender, 2, array('class' => 'form-control')) }}
If you already has a $user with sex_id attribute(or your sex column), You can pass it to the Form::select as well.
{{ Form::select('gender', $gender, $user->sex_id, array('class' => 'form-control')) }}
http://laravel.com/docs/4.2/html#drop-down-lists