Laravel obtain the selected value from a Form::Select - php

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

Related

Laravel 5.5 correct way for a CRUD form select

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.

Laravel 5 | Select to variable

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'));

perplexing check box behavior

I have a edit form with some checkboxes that I am trying to make checked when the associated many to many relationship is established.
Distributors belong to many beers
Beers belong to many distributors
in my controller I have:
$breweries = Brewery::lists('name', 'id');
$all_dist = Distributor::all();
$beer = Beer::find($id);
$distributions = [];
foreach ($beer->distributors as $distributor)
{
$distributions[$distributor->id] = BeerDistribution::where('beer_id', '=', $beer->id)
->where('distributor_id', '=', $distributor->id)->first()->price;
}
return View::make('beers.edit', ['beer' => $beer, 'distributors' => $all_dist, 'distributions' => $distributions, 'breweries' => $breweries, 'styles' => $styles]);
and I in my edit form I have:
{{ Form::model($beer, ['route' => ['beers.update', $beer->id], 'method' => 'PATCH']) }}
#foreach ($distributors as $distributor)
<?php $carried = in_array($distributor->id, array_keys($distributions)) ? true : false ?>
{{ Form::checkbox('distributors[]', $distributor->id, $carried); }}
{{ Form::label($distributor->name) }}
{{ Form::label('price' . $distributor->id, 'Retail:') }}
<?php $price = $carried ? $distributions[$distributor->id] : null ?>
{{ Form::text('price' . $distributor->id, $price ) }}
#endforeach
{{ Form::submit('Save') }}
{{ Form::close() }}
Basically I am passing an associated array of each distributor_id => price. This array also tells me which distributors the beer already belongs to so that I can mark those checked off in my edit form.
Here's where things get wonky. When I load this form, all the checkboxes will be checked no matter what. If I change my controller loop to this:
foreach ($beer->distributors()->lists('distributor_id') as $distributor_id)
Then I can do create my array.
Why does calling $beer->distributors in the controller would result in all the checkboxes being checked?
The problem is with the last argument : $carried
{{ Form::checkbox('distributors[]', $distributor->id, [ "checked" => $carried ]); }}
Pass the last parameter as array, and tell it exactly which attribute to modify and the attribute value.

Laravel 4 display list of data into form selection

How do I display a list of books in laravel form builder selection?
BookController.php
$book_names = Book::all();
return View::make('books')->with('book_names', $book_names);
At the moment I only know how do manually input data:
{{ Form::select('book_name', array(
'book1' => 'book1',
'book2' => 'book2',
'book3' => 'book3')
}}
I want to do something like this:
{{ Form::select('book_name', array(
#foreach($book_names as $book_name)
$book_name->name => $book_name->name,
#endforeach
}}
But obviously It won't work..
Meet the lists() method. It allows you to create an array from one or two (key and value) properties of a collection:
$book_names = Book::lists('name');
return View::make('books')->with('book_names', $book_names);
And then simply pass that array:
{{ Form::select('book_name', $book_names) }}

Laravel form model binding for many to many select input

I have a many to many relationship between clients and assets.
For my edit client form I have multi-select box with which the user can select several assets to attach to a client. The form is using model-binding so it automatically populates the fields with the existing client data.
The model binding works for all fields in the form except the multi-select. Here is a snippet from my view:
{{ Form::model($client, ['route' => ['clients.update', $client->id], 'class' => '', 'method' => 'put']) }}
{{ Form::label('name', 'Name', $label_attributes) }}
{{ Form::text('name', null, array('class'=>'form-control')) }}
{{ Form::label('assets', 'Client Benchmarks (Select multiple)', $label_attributes) }}
{{ Form::select('assets[]', $assets, null, array('multiple' => true, 'class' => 'form-control')); }}
When I submit the form the relationship saves the data successfully via the sync method in my controller (update method):
$client = Client::find($id);
$client->name = Input::get('name');
$assets = Input::has('assets') ? Input::get('assets') : array();
$client->assets()->sync($assets);
$client->save();
Also, if I output the client assets directly they are accessible in the model collection in my form view:
<?php print_r($client->assets); ?>
How can I get the form to populate the select box with the existing selections?
echo Form::select('assets[]', $assets, array(1,2), array('multiple' => true));
As far as I know it won't set all selected values by default for you even when using form
model binding
This is what the native formBuilder method looks like:
public function select($name, $list = array(), $selected = null, $options = array())

Categories