Retrieve old value from Laravel Form::select - php

I have the following form field in a Laravel project in my create view (create.blade.php):
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), null, ['class' => 'form-control format']) }}
I retrieve this inside the format column in my database so i can use this data in my project.
Now, inside the edit (edit.blade.php) view I want to have the already selected data to reflect. So when the user has selected "video", the select option will already be set to Video.

While I was typing this question, i figured it out. Already typed the entire question so might as well post it to help someone out.
The third argument needs to correspond with the data I retrieve from the database. So, for example, if i wanted the Video to be selected, it would look like this:
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), 'is_video', ['class' => 'form-control format']) }}
I replaced the is_video with the variable that contains the data from my database and it's working as expected.
{{ Form::label('format', 'Type', ['class'=>'label']) }}
{{ Form::select('format', array('is_html' => "HTML", 'is_video' => 'Video'), $var->format, ['class' => 'form-control format']) }}

Related

isset for form builder

I am using form builder and I share one form for edit and crete page so there are some variables I don't want to use.
I know that:
{!! Form::text('name', isset($admin_link->name) ? $admin_link->name : null, ['class' => 'form-control']) !!}
would work but I do not consider it the best way. Is there any way to use something else like isset for form builder?
I sent the model to the blade with values what caused autocomplete of the input what is probably what I have been looking for:
{!! Form::model( $admin_link, ['route' => ['admin.links.update', $admin_link->id], 'method' => 'post', 'class' => 'main_link_form']) !!}

Laravel 5.2 Dropdowns with Eloquent Lists Method

I am pulling some values ($citylist = User::lists('city');) from my DB to display them as a dropdown list.
This is my view:
{!!Form::open(array('action' => 'PagesController#menue', 'method' => 'GET', 'style' => 'display: inherit;'))!!}
{!! Form::select('city', $citylist, null, array('class' => 'selectpicker input-group-btn form-control', 'data-style' => 'btn-info btn-info btn-block')) !!}
<span class="input-group-btn">
{!!Form::submit('Submit', array('class' => 'btn btn-info'))!!}
</span>
{!!Form::close()!!}
After submitting the form there is a redirect. I am appending a city name as a query string to the url. So I get something like .../menues/?city=london.
However, since I am pulling the values with Eloquent's lists() method the ID is appended to the url instead of the city name.
So I get something like .../menues/?city=1 instead of .../menues/?city=london.
I need the city name. How can I fix this?
Try to build list like this (I'm using pluck() since lists() is deprecated):
$citylist = User::pluck('city', 'city');

Blade select input - selling a selected option

How do I set a certain option as currently selected?
My current code is:
{{ Form::select('make',$makes , NULL, ['class' => 'form-control']) }}
$makes is an array of car makes.
Lets say the array looks like:
[
0 => 'Audi',
1 => 'BMW',
2 => 'Mercedes'
]
If I am editing a car, I know that in my DB I have said that car is X make.
In this case, lets say its a BMW. When I create this select input I want BMW to already be shown as the selected option. I have this value already, its just getting the blade input to set it to selected.
I tried:
[$tMake->display_name] + $makes
But that just add a new option to the list, which is not what I want.
Thanks.
You need to tell the select the ID you want pre-selected.
You can do this:
{{ Form::select('make',$makes , $tMake->id, ['class' => 'form-control']) }}
And if you are using validation - you'll want to do something like this, so that the old input is shown if the validation fails
{{ Form::select('make',$makes , Input::old('make', $tMake->id), ['class' => 'form-control']) }}

How to retrieve serialized data in Form::model and populate array of Form::select with multiselect in laravel

I have a serialized data saved in my database, it's a values from array of categories. I created this because it's easy to manage what categories are selected.
So for creating a "page" i created create.blade.php page and form for multi select categories.
{{ Form::open(['role' => 'form', 'method' => 'post', 'route' => 'admin.games.store', 'files' => true]) }}
{{ Form::select('categories[1][]', $platform, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[2][]', $genre, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[3][]', $developer, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::close() }}
I defined $developer, $genre and $platform in my controller, this is basically a list of categories under specific id.
$platform = Category::->where('type', '=', 1)->lists('name', 'id');
So this returns an array of all categories for a view.
----------------
| name | id |
----------------
| Windows | 1 |
----------------
| Linux | 2 |
----------------
| MacOS | 3 |
----------------
So this all works fine i have my select forms working fine with categories i specified and upon submit i get an array of values, as array can't be saved i serialized that very simple with:
serialize(Input::get('categories')
And i have serialized data in my database, i know someone would say it's not a good way if i need to search etc... i will not be able to use that field. But for me i need only to store id's of selected categories and query them all together at once. so this is good for me.
But now i have a problem, i am not finding a way to get that data when i edit a page.
I use Route::controller so you know work flow how the pages are updated, stored, edited, created... And since form can automatically populate the fields using Form::model i used that before and it's very nice feature. But i don't know how to populate the fields now when i edit the page.
Here is a form on my edit.blade.php
{{ Form::model($game, ['role' => 'form', 'method' => 'PUT', 'route' => ['admin.games.update', $game->id ], 'files' => true]) }}
{{ Form::select('categories[1][]', $platform, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[2][]', $genre, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::select('categories[3][]', $developer, null, ['multiple'=>true, 'class' => 'form-control']) }}
{{ Form::close() }}
So how do i get that serialized data and populate the select fields with selected categories when editing a page using Form::model. Any ideas, i searched on net and no answers.
I think this should tell you what you need to know: Getting selected values from a multiple select form in Laravel
So instead of passing in a null third parameter you instead pass in an array of selected values.

Laravel PHP: Keep the chosen dropdown option stay selected on View

I have a homepage that contains a dropdown menu that allows the user to select a category and will display results depending on which option is chosen from the dropdown menu.
It is currently working fine in updating and displaying the correct results but now I've run into a minor issue where I want the dropdown to stay selected on the chosen category. Normally I would put a simple line of code within my view such as
{{ Form::label('category', 'Category:') }}
{{ Form::select('category', array('option1' => 'Option1', 'option2' => 'Option2'), $video->category) }}
where $video is the model used in the controller.
However this situation is a little bit different because I need to pass the 'category' variable from within my controller so that the dropdown menu will stay on the selected category after the user makes their choice.
Controller:
public function index()
{
$vdo = Video::query();
$pic = Picture::query();
if($category = Input::get('category')){
$vdo->where('category', $category);
$pic->where('category', $category);
}
$allvids = $vdo->paginate(10);
$allpics = $pic->paginate(10);
$data = compact('allvids','allpics');
$this->layout->content = \View::make('home.pics_vids_overview',$data)->with('category', Input::get('category'));
}
View:
{{Form::open(array('route' => 'overview_select', 'files' => true)) }}
<div class="form-group">
{{ Form::label('category', 'Category:') }}
{{ Form::select('category', array('Category1' => 'Category1', 'Category2' => 'Category2', 'Category3' => 'Category3', 'Category4' => 'Category4'), array('class' => 'form-control')) }}
I've tried several approaches in passing the chosen 'category' variable back to the dropdown so that it will stay on the chosen option after the user makes their choice but none of them have worked for me yet. Any help is greatly appreciated!
You may try this:
{{
Form::select(
'category',
array('Category1' => 'Category1', 'Category2' => 'Category2'),
(isset($category) ? $category : 'Category1'),
array('class' => 'form-control')
)
}}
Use Form::model instead of Form::open to bind the model to the form and it will automatically pick up any values in the model:
{{ Form::model(array('route' => 'overview_select', 'files' => true)) }}

Categories