Drop down select form in Laravel - php

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

Related

Select input with Laravel Collective

{!! Form::select('country',['1' => 'Albania','2'=>'Kosovo','3'=>'Germany','4'=>'France'],null, ['class'=>'form-control','placeholder'=>'Select Country']) !!}
This type of input doesn't take the values of the array but instead, it takes the pre-default value of option inside select.enter code here
According to the Form Collective documentation, the first argument of the Form::select() is the name of the select box, the second is an array of the input values while the third is the default value (this could be set to null). The fourth is an optional array of attributes/values.
This should work for you.
{!! Form::select('country',['Albania' => 'Albania','Kosovo'=>'Kosovo','Germany'=>'Germany','France'=>'France'],'Kosovo',['class'=>'form-control','placeholder'=>'Select Country']) !!}
{!! Form::select('country', [null => 'Select Country'] + ['Albania' => 'Albania','Kosovo'=>'Kosovo','Germany'=>'Germany','France'=>'France'], null, ['class' => 'form-control']) !!}
For more information : https://laravel.com/docs/4.2/html#drop-down-lists

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

Laravel returns htmlentities() expects parameter 1 to be string, object given

I have the following Laravel 5.1 controller function
public function editare($prod_id) {
$categorii=DB::table('categorii_produse')
->select('cat_id')
->get();
$categorie_selectata=DB::table('produse')
->leftjoin('categorii_produse','prod_cat_id','=','cat_id')
->where('prod_id','=',$prod_id)
->select('prod_cat_id')
->get();
$articole=DB::table('produse')
->leftjoin('imagini','prod_id','=','img_prod_id')
->where('prod_id','=',$prod_id)
->get();
return view ('pagini.editare',compact('categorii','categorie_selectata','articole'));
And the following line in the view which has problems
{!! Form::select('categorii',$categorii, null, ['class' => 'form-control']) !!}
The view returns the following error
htmlentities() expects parameter 1 to be string, object given
As 2nd argument you need to pass array in format value => displayed option (in your case I see you only use cat_id as both value and displayed option), so instead of:
{!! Form::select('categorii',$categorii, null, ['class' => 'form-control']) !!}
you should use:
{!! Form::select('categorii',collect($categorii)->lists('cat_id')->all(), null, ['class' => 'form-control']) !!}

Laravel not letting me add styles to my Form::text()

This is my code for the form builder, and when I remove the array from the text() everything is fine, but when I add it I get this error
ErrorException in helpers.php line 454: htmlentities() expects
parameter 1 to be string, array given (View:
/Users/samir/Sites/elicant/resources/views/pages/classes.blade.php)
{!! Form::open(array("url" => 'addClass', "method" => 'post')) !!}
{!! Form::text('classname', array('style' => 'width:80%;')) !!}
{!! Form::text('classcolour') !!}
{!! Form::submit("Add") !!}
{!! Form::close() !!}
What have I done wrong?
Form::text has 3 parameters: $name, $value, array $options = array()
So to make your code working:
{!! Form::text('classname', null, array('style' => 'width:80%;')) !!}

Categories