I'm having problem on setting the default value of "Please Select" on my select form from laravel. here comes the code.
$user = \App\User::where('role_id','=','3')->orderBy('name', 'ASC')->lists('name','staff_id');
and here's on my blade
{!! Form::select('requestby', $user, Input::old('requestby'), array('class' => 'form-control')) !!}
i have tried to put the array_merge but it seems like it is overwriting the <option> value from staff_id to index value. what should i do now?
array_merge will re-index the array when merging. You can use + for this -
$user = array('' => 'Please Select') + $user;
The indexs will not be changed.
Related
I didn't get the select option from controller
it's my Edit page
{!! Form::select('channel_id',[''=>'Select A Channel'] + $channels,null,['class'=>'form-control']) !!}
Below is my Controller
public function edit($id)
{
$channels = Channel::all() ;
$d = Discussion::findOrFail($id);
return view('discussion.edit',compact('channels','d'));
}
Form::select is only meant to take a an array of key value pairs for setting the options.
What you can do is use the pluck() method:
[''=>'Select A Channel'] + $channels->pluck('name', 'id')->toArray()
or
$channels->pluck('name', 'id')->prepend('Select A Channel', '')->toArray() // I'm not sure if you will need `->toArray()` here
If you're not going to be using the $channels for anything else on the page then you'll be able to use the pluck() in the controller instead:
$channels = Channel::pluck('name', 'id');
Then in your blade file you can do:
[''=>'Select A Channel'] + $channels->toArray()
or
$channels->prepend('Select A Channel', '')->toArray()
You will need to change the name and id placeholders I've used with pluck() to match the column names from your channels table.
Lastly, depending on the behaviour you want from the placeholder for your <select> you can actually set it by doing:
{!! Form::select('channel_id', $channels->pluck('name', 'id'), null, ['class'=>'form-control', 'placeholder' => 'Select A Channel']) !!}
Notice the 'placeholder' => 'Select A Channel'] in the last array:
https://laravelcollective.com/docs/5.3/html#drop-down-lists
I am using form model binding and have the following select input :
{!! Form::select('user_id', $users, old('user_id') ?: Auth::id(), ['class' => 'form-control select2 users']) !!}
I would like to accomplish the following:
In the creation form: select the option where the user_id equals the authenticated user's ID, but if there was old input in the session, select that option instead.
In the edit form: always select the option that is stored in the model.
old('user_id') ?: Auth::id() doesn't seem to work when editing, because it always selects the option of the authenticated user and not the one that is stored in the model.
I believe you already have this line,
{!! Form::Model($users, ['route' => ['admin.user.update', $user->id], 'method' => 'PUT']) !!}
then your code should like this:
{!! Form::select('user_id', $users, isset($selectedUser) ? $selectedUser : null) !!}
at Create function do not pass $selectedUser to view but pass the id in your session,and in your edit function you should pass $selectedUser to view ,which is Auth::user()->id
I have run into a roadblock with getting the value of the Select.
Please have a look at my form:
{!! Form::open(array('action' => 'MasterController#datamaker')) !!}
{!! Form::select('Area', array('Science', 'Arts',)); !!}
{!! Form::input('date', 'Year') !!}
{!! Form::submit('Add') !!}
Here is my (simplified) method in the controller:
public function datamaker() {
$input = Request::all();
$Database = new Database;
$Database -> Area = Request::get('Area');
$Database -> Year = $input['Year'];
$Database -> save();
return (Request::get('Area')); <----- This is returning "0" instead of the values declared in the form
}
I have checked other sources and the solutions they have provided have not worked. Whatever I have tried, it always just returns "0" instead of the values declared in the form.
Can anyone point me in the right direction?
Thanks!
The reason why you are getting 0 instead of Science is that Form component, when generating a select, uses array keys as values of available options and array values as string that is displayed in the select. You don't provide array keys, so the keys are 0 for Science and 1 for Arts.
If you want to get Science/Arts as the value from Request::get('Area') you need to pass an array to Form::select() where values and keys are the same strings, e.g.
Form::select('Area', ['Science' => 'Science', 'Arts' => 'Arts']);
I have a form in which the user can optionally select an option and if the user does not choose an option I need the default value of 'null' be submitted to database.
I am using laravel 5.1 so my input field is as follows:
<div class="form-group col-lg-4">
{!! Form::label('cat1', 'CAT Tool 1', ['class' => 'control-label']) !!}
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-cog"></span>
</div>
{!! Form::select('cat1', $cats , null , ['class' => 'form-control']) !!}
</div>
</div>
Here the $cats array is an array of CAT Tools ($cats=['SDL Trados', 'Deja Vu', 'WordFast', 'OmegaT', 'Fluency'];). I have similar fields in which an array of database attributes are returned (e.g. In the controller create method I have $languages=Language::lists('fa_name', 'id');). How can I accomplish this?
The listsmethod used to return an array but as of Laravel 5.1 it returns a collection so I needed to convert the $languages to an array like this:
$languages=Language::lists('fa_name', 'id')->all();
or
$languages=Language::lists('fa_name', 'id')->toArray();
Next in the view I can use the following:
{!! Form::select('from1', [null => 'Select your language'] + $languages, null , ['class' => 'form-control']) !!}
which will add an empty option to the select field.
I don't know about having it submit as null. If you put a blank one in there, it will just come back as an empty string so you would have to check that it's empty and if it is, set it to null.
To add it to the array, simply add it to the beginning and it should work.
$cats = ['', 'SDL Trados', 'Deja Vu', 'WordFast', 'OmegaT', 'Fluency'];
Or if you already have the $cats variable and you need to modify it...
array_unshift($cats, '');
You may have to key these arrays though because I believe the select is going to set the keys as the text and the values as the value in the option elements.
In that case, you will want to still use array_unshift and pass in an array instead of a string.
array_unshift($languages, ['Optional' => '']);`
That will add an additional option to the select with the test Optional and no value.
I've created a very simple form that has some select values and when you select one of the items from the list and hit submit, it goes to another page that imports a file template to confirm your selection. For some reason on the next page, instead of displaying the item name that was selected, only the row ID of the select value pops up. Is there an additional option that I need to pass through to get the value displayed in the list?
Here's my create.blade.php
<div class="form-group">
{!! Form::label('candy_flavors', 'Candy Flavors:') !!}
{!! Form::select('candy_flavors', array('' => 'Select Flavor') + $candy, null, ['class' => 'form-control'])!!}
</div>
Here's my CandyController.Php
public function create()
{
$candy = Candy::all()->lists('name');
return view('candy.create', compact ('candy'));
}
public function confirm(Requests\PrepareCandyRequest $request, Guard $auth)
{
$candytemplate = $this->compileCandyRequestTemplate($request->all(), $auth);
return view('candy.confirm', compact('candytemplate'));
}
public function compileCandyRequestTemplate($data, Guard $auth)
{
$data = $data + [
'name' => $auth->user()->name,
'email' => $auth->user()->email,
];
return view()->file(app_path('Http/Templates/candytemplate.blade.php'), $data);
}
Here's my candytemplate.blade.php
#extends ('master')
#section ('content')
This is your candy selection: {{ $candy }}
#endsection
In confirm you can just call:
$request->get('candy_flavors')
However it will return blank, because it is attempting to return you the "value", not the "display value" of the select box. And in your case you are passing in an array of empty keys. array('' => 'Please select', '' => 'name 1', etc).
The form builder class uses the keys of the array to fill in the values.
$candy=['apple'=>'apple','banana'=>'banana','mango'=>'mango'];
and now use
<div class="form-group">
{!! Form::label('candy_flavors', 'Candy Flavors:') !!}
{!! Form::select('candy_flavors', array('' => 'Select Flavor') + $candy, null, ['class' => 'form-control'])!!}
</div>
you have to specify key and value otherwise select will set value from 0
So what I decided to do was make the call from my controller, and pass through the label twice. This did the trick
$accountexecutive=AccountExecutive::lists('flavor', 'flavor');
return view('candy.create', compact ('flavors'));