SelectRaw in blade template to create dropdown list - php

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.

Related

Fom::select in laravel empty for default

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>

Laravel model binding for select dropdown

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']) !!}

Get the id of a selected value in Selectbox Laravel 5.1

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>

List in Laravel with more than 2 column

Using Laravel 5, I'm trying to make a list of countries in a dropdown.
I use this syntax :
$countries= Countries::get('name', 'id');
And then, with blade
{!! Form::select('countries', $countries) !!}
This gives me a nice list, but I would like to set a custom attribute on every country. This argument is the continent of the country, something like "data-continent='X'".
This continent is a column of my table "countries".
Add the continent column to your query
$countries= Countries::get('name', 'id', 'continent');
Then loop over the results and ouput using any format you prefer.
<select name="country">
#foreach ($countries as $c)
<option data-continent="{{ $c->continent }}" value="{{ $c->id }}">{{ $c->name }}</option>
#endforeach
</select>

Add Selected in Dropdown Option in Laravel

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

Categories