Here is my Select Box, Here All Company will be loaded,
But i want to display the Particular company as default selected which i have it in session.
Here is my Code
$sessioncompany = 'ABCcompany'
$comp[''] = 'Company';
#foreach($company_list as $row)
<?php
$comp[$row->CompanyID] = $row->CompanyName;
?>
#endforeach
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID')); }}
What i tried is
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID'), array(id=>$sessioncompany)); }}
But i ended with failure
What is the mistake i am doing and How can i fix that ?
Note : I want it in the native laravel method
$selectedId = ... // get the id from the session
{{ Form::select('CompanyID', $comp, array($selectedId), array('id' => 'seCompanyID')) }}
The Form::select() method has the following parameters:
$name - the nameHTML attribute
(array) $list - the list of options, key-value pairs for actual value => displayed value
$selected - the selected item's value
(array) $options - any extra HTML attributes, as per other Form/HTML helper methods
So, the third parameter $selected is the one you want to pass your session value to. Copying your example and replacing:
{{ Form::select('CompanyID', $comp, Session::get('my_session_var'), array('id' => 'seCompanyID')); }}
Where 'my_session_var' is the name of the session variable you store the value of the item that should be selected by default.
The great thing about using Laravel's Form helpers is that you get the 'old' input for free. For example, if you set a default here and the user changes it, submits the form and there's a validation error. When the form is redisplayed (assuming you redirected back with return Redirect::back()->withInput();) the value the user selected, rather than your explicit default will be set.
(Thanks to #JarekTkaczyk for putting me right on the old input thing.)
You are passing wrong parameters. max parameters should be 4. Try this:
{{ Form::select('CompanyID', array('default' => $sessioncompany)+$comp, 'default') }}
Just beware, if you're on a page with the form with the select box, make sure that with each page refresh while testing you do a full page refresh without using cache - Ctrl + Shift + R, otherwise the last selected value may remain selected.
Related
Pic 1
Pic2
The situation looks like this.
When sorting, I choose options "Like" and click submit. Everything is sorted nicely, but after refreshing the page, the value in the field is "Lastest".
Is there any possibility that the value I chose would remain until the next select.
You can use a session to keep the last selected value as the default value.
Here you can learn about the sessions.
I fixed it.
I add this to select:
<option href="/?sortMypost=1" value="1" {{ $option == '1' ? "selected" : "" }} >Latest</option>
And this to controller:
$option = request()->input('sortMypost');
And this to the end of the controller:
return view('posts.mypost', compact('posts', 'option'));
Hi I send a form in my contact.blade.php. I read in order to use the PUT method you have to create a hidden input field which contains the method.
#if($do == 'edit')
{{ Form::model($contact, array('method' => 'PUT', 'route' => array('contact.update', $contact->id), 'id' => $do=='edit' ? $do.$contact->id : $do.$contact_type_id, 'form_id' => $do=='edit' ? $do.$contact->id : $do.$contact_type_id)) }}
{{ Form::hidden('_method', 'PUT') }}
#endif
....
{{ Form::submit('speichern', array('class' => 'btn btn-primary')) }}
</div>
{{ Form::close() }}
The route:
Route::put('/contact/{id}', array(
'uses' => 'ContactController#update',
'as' => 'contact.update'
));
The Controller:
public function update($id)
{
dd(Input::all());
// //get user account data
// $user = User::find( Auth::id() );
// // validate input
// $v = Contact::dataValidation( Input::all() );
return Redirect::Route('user.edit', 1)->withSuccess("<em>Hans</em> wurde gespeichert.");
Q1:
As soon as I call dd(Input::all()); I don't get redirected any more, instead I see a json with my form values.
Q2:
I'm just debugging this so I didn't program it. So my second question is:
From my understanding dd(Input::all()); gets all my form data. So don't I need to store it anyways somewhere?
Q1: dd() terminates the script, hence why you are not getting redirected. It's used as a tool to essentially break and examine what is going on.
http://laravel.com/docs/4.2/helpers
Q2: You will still need a model to feed the Input::all data into. Input::all simply fetches the submitted data, it doesn't do anything with it. It ultimately depends on your use case, sometimes you may want to email the data, but obviously most times you would what to store it against your persistence layer (read database / datastore)
Question 1
when you use DD, it will show the data and stop at that line.
DD
Dump the given variable and end execution of the script.
more information you can read it here DD in DD session.
Question 2
I'am not sure about 2nd question but if you want to get value from all input you could us Input::all();
more information All input in Getting All Input For The Request session
I'm trying to figure out how I can structure my Laravel select menu so that it shows up as this for a final render. Has anyone done such a thing.
The location is a property of the arena object.
<option value="arena_id">Arena Name - Location</option>
{{ Form::select('arena_id', [ null => 'Please Select'] + $arenas, null, ['id' => 'arena_id']) }}
I asssume $arenas comes from something like Arena::where('foo', bar)->get(), but with get() you will get an instance of Illuminate\Database\Eloquent\Collection instead of an actual array which is what you want in Form::select.
So what you need to do is to use lists($field, $key), it will fetch you rows and return it as an array.
$arenas = Arena::where('foo', bar)->lists('name', 'id');
There is a code example here with some comments from users if you want to learn more.
You can use pluck function for getting the results as array
refer https://laravel.com/docs/5.1/collections#method-pluck
$select = $this->all()->pluck('title', 'id');
Then you can use below sample code for creating select box with selected option in blade template
{{ Form::select('name',$select,'selected option id',['class' => 'form-control']) }}
This is example form code:
{{Form::checkbox('selection[]', '1')}}
{{Form::checkbox('selection[]', '2')}}
{{Form::checkbox('selection[]', '3')}}
{{Form::checkbox('selection[]', '4')}}
This is example code for saving:
$selection = json_encode(Input::get('selection'));
It will be then save into MySQL table in 'selection' column.
So now, how can I retrieve the data into the form edit mode?
Thanks.
$selections = json_decode($selectionFieldFromDB); // pass it to the view
// then just:
#foreach (range(1,4) as $i)
{{ Form::checkbox('selection[]', $i, in_array($i, $selections)) }}
#endforeach
Mind that if you have an attribute on your model selection, then Laravel will ignore in_array() part, since it first checks the values on the bound object.
So if you want it to work like I suggested, don't use selection name for the checkboxes if that's the name of your model attribute.
so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))