Laravel 5.2 Dropdowns with Eloquent Lists Method - php

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

Related

Laravel - Select input not keeping old value

I have a piece of code:
{!! Form::select('option_employee_review', old('option_employee_review', $employeeReviews), $employeeReviews, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
It saves the value to the database correct. When i go to edit the item again the select input does not keep the old value that's in the database. How do i make it so that the select input does keep its old value.
$employeeReviews:
[
2843 => "Medewerker review 1"
2849 => "Medewerker review 2"
]
I am not using your syntax, but something like this will do I think.
<option value="{{$channel->id}}" {{ (old("channel_id") == $channel->id ? "selected" : "" ) }}>{{$channel->title}}</option>
Second parameter to select function must be array of options.
Try changing it like this
{!! Form::select('option_employee_review', $employeeReviews, old('option_employee_review', $employeeReviews), ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
Or based on your parent object let's say employee you can try
{!! Form::select('option_employee_review', $employeeReviews, $employee->option_employee_review, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}

Retrieve old value from Laravel Form::select

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

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

Drop down select form in Laravel

may I know what is wrong with my codes? I have three user type name registered in my database however my codes will result to three drop down menus with individual user type name on each.
#foreach($user_types as $usertype)
<div class="form-group">
{!! Form::select('chap_user_type_name', array('chap_user_name' => $usertype), null, ['class' => 'form-control']) !!}
</div>
#endforeach
remove foreach and pass array into select like this
{!! Form::select('chap_user_type_name', $user_types, null, ['class' => 'form-control']) !!}
If $user_types is a collection, you need to use pluck() to build correct array for ::select:
$user_types = UserTypes::pluck('name', 'id');
Then just build select element like this:
{!! Form::select('chap_user_type_name', $usertype, null, ['class' => 'form-control']) !!}
You don't need the foreach above form::select will take care of it

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.

Categories