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'));
Related
So I have this CRUD where I use the same form to create and edit entries.
I need in several form selects, when creating (no data present for that particular field yet) my select to show the placeholder, but when editing, my select to show whatever is stored on database for that particular id filed. So I have:
Controller:
...
public function create()
{
$house = houses::pluck('name', 'id');
//$thisclient = null;
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
return view('prospects.create', compact('house', 'clients', 'reps'));
}
...
public function edit($id)
{
$house = houses::pluck('name', 'id');
//$thisclient = user::whereId($id)->first();
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
$prospect = Prospect::findOrFail($id);
return view('prospects.edit', compact('prospect', 'house', 'clients', 'reps'));
}
and my view form:
Working for create:
{!!Form::select('client_id', $clients, null, ['class' => 'form-control', 'placeholder' => 'Please Select'] ) !!}
Working for edit:
{!! Form::select('client_id', $clients, $prospect->client_id, ['class' => 'form-control'] ) !!}
I'm having 2 troubles here, if I have null as my selected field, it won't bring the selected data on edit, if I have $prospect->client_id , it will return a error on create as there's no data yet.
I tried to solve this by creating a variable $thishouse on controller and passing it to view on return view('prospects.create', compact('house', 'thisclient','clients', 'reps')); and view Form::select('client_id', $clients, $thisclient, ['class' => 'form-control'] ) !!} but seems a bit dirty whne having several form selects...
The second trouble is if I leave a placeholder on Edit, it will show the placeholder, not $prospect->client_id itself.
What's the best and simplest way to achieve all of this and use the same form for create and edit?
Thanks
You can use Form::open and Form::model to create and edit. As an example, you can set in your view:
#if(isset($prospect))
{!! Form::model($prospect, ['action' => ['ProspectController#update', $prospect->id], 'method' => 'patch']) !!}
#else
{!! Form::open(array('action' => 'ProspectController#store', 'method' => 'POST')) !!}
#endif
And then you can create the select like this:
{!! Form::select('client_id', $clients, old('client_id'), ['class' => 'form-control'] ) !!}
So, when you are editing, Laravel will select the attribute from the variable on model function.
And since you are using Laravel 5.5, you could also use #isset instruction.
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'm working with the following code. Trying to utilize the model binding effects of Laravel and the FormBuilder.
{!! Form::model($contact,['method' => 'PUT', 'url' => URL::route('contact.put', $contact),'accept-charset' => 'UTF-8', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{!! Form::label('','First Name') !!}
{!! Form::text('first_name','', ['class' => 'form-control','required' => 'required']) !!}
</div>
...
{!! Form::close() !!}
This is pretty stock code, which is taken from the older documentation, it will fine be able to update the information as intended, but there is no way i can get the model binding to fill the value of the Text Input Field first_name. The weird part is, if i create it in stock html code, like this:
<input type="text" class="form-control" name="first_name" id="first_name" value="{{$contact->first_name}}" required="required">
It will fill out the value fine, so what is missing?
It's because you are passing in an empty string. Unless you have a predefined value that you want to set, you should pass a null value like this:
{!! Form::text('first_name', null, ['class' => 'form-control','required' => 'required']) !!}
Why is this happening?
This is the section of the code that populates the input's value:
public function getValueAttribute($name, $value = null)
{
if (is_null($name)) {
return $value;
}
if (! is_null($this->old($name))) {
return $this->old($name);
}
if (! is_null($value)) {
return $value;
}
if (isset($this->model)) {
return $this->getModelValueAttribute($name);
}
}
What you should focus on is the 3rd if statement. You are passing in an empty string, which is not a null value, so it assumes you know what you want to pass as the default value.
However, if you pass in a null value, it skips the 3rd if statement and moves onto the 4th if statement, which retrieves the value from the model.
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
Hello I have an array with name $crit where I put the value of my HTML form components
for example this input:
{{ Form::label('name', 'Name:', array('class'=>'label'))}}
{{ Form::text('name', isset($criteria['name'])?$crit['name']:'', array('id'=>'name', 'class'=>'input'))}}<br/>
I want to do the same with the select component but I don't know how to add the value of the selected sex to my $crit array.
{{Form::label('Sex', 'Sex:', array('class'=>'label'))}}
{{Form::select('sex', $options, 'M', array('id'=>'sex'))}}
On my view I have defined the array $options
public function get_index()
{
return View::make('personal.data')
->with('options', array('M' => 'Male', 'F' => 'Female'))
}
Thanks for your answer, Regards
You just use Input::old and it'll be re-populated on redirect/error
{{ Form::select('sex', $options, Input::old('sex')) }}