I want to pass get all existing locales to view. This is my code
view
{!! Form::select('language', $languages,null, ['placeholder' => 'Pick a language']) !!}
controller
this only pull the current how can I pull all with eloquent
$languageCurrent = App::getLocale();
How can I pass it into view(when I'm manipulating data from database I can return with something like this)
->with('users', $users)
How can I return value as array
If you have multiple locales defined in config/app.php, like described here:
'locales' => ['en' => 'English', 'sv' => 'Swedish'],
You could try to do this:
{!! Form::select('language', array_flip(config('app.locales')), null, ['placeholder' => 'Pick a language']) !!}
config() will get locales list and array_flip() will swap keys and values for Form::select.
You can add an array in /config/app.php containing the locales which you use, for example : 'locales' => ['en' => 'English', 'pl' => 'Polish'] than you should be able to use config() helper function to get the values like $available_locales=config('app.locales');
Related
This question already has answers here:
passing data to a view laravel
(2 answers)
Closed 3 years ago.
I am beginner with using Laravel.
I would ask you about pass data from DB to view.
Code:
{!! Form::select('unit_id', $units, null, ['class' => 'form-control']) !!}
In controller i am trying get value from DB and pass to view.
$units = DB::table('units')
->select('id', 'name')
->get();Title
$title = 'Create New Dets';
return view('dets.create', ['title' => $title, 'units' => $units]);
I am tried with compact etc.
Maybe i should use Model? This table is only for units.
Could someone explain me how i should do it?
Many thanks for that.
To pass the values to blade view you have to
return view('dets.create', compact( 'title', 'units'));
you can do the following:
$units = DB::table('units')
->select('id', 'name')
->pluck('name','id');
$title = 'Create New Dets';
return view('dets.create', ['title' => $title, 'units' => $units]);
Did you check the $units? I mean check it whether it has data or not.
dd($units);
if it has data you can pass it in multiple ways i.e. using compact, as an array in the form of
key=value
compact case:
return view('dets.create', compact('title', 'units'));
array case:
return view('dets.create', ['title' => $title, 'units' => $units]);
You have your data in the form of key and value
{!! Form::select('unit_id', ['L' => 'Large', 'S' => 'Small'], null, ['class' => 'form-control']) !!}
in controller
$units = Units::all()->pluck('name', 'id')->toArray();
Hi I'm using Laravel version 5.6 and got a problem in view with Form::select.
I already made some Form::open with 'text' and 'textarea' they all worked fine, but the Form::select do not generate the select fild on my view.
I used this code:
{!! Form::label('isPropaganda', 'Propaganda:') !!}
{!! Form::select('isPropaganda', ['Não' => '0', 'Sim' => '1'], null, ['class'=>'form-control','multiple']) !!}
I found the example here http://laravel-recipes.com/recipes/163/creating-a-select-box-field but didn't workout. How can I fix it?
Try flipping the keys and values. Keys cannot have special characters in them. Also, I'm not sure what that 'multiple' attribute is doing there. If it's meant to be a css class, place it inside the 'class' array key, otherwise add it to a new attributes array.
{!! Form::select('isPropaganda', ['0' => 'Não', '1' => 'Sim'], null, ['class'=>'form-control multiple']) !!}
I have the following code in my view:
{!! Form::select( 'projectSkills',
['' => ''] + \App\Skill::where('type','freelancer')
->pluck('name', 'id')
->toArray(),
#$skills,
[
'class' => 'select2',
'multiple'=>'multiple'
]
) !!}
Where the value of the options comes as 0,1,2,3,4,5,
I want the value as a name which is fetching from the table.
What am i doing wrong there ?
You are making a lists consisting of: [id => name] by calling pluck('name','id')
This will be fed into the form causing a dropdown list:
<option value="id">name</option>
If you wish to change the 'value' part, you need to supply an array consisting of the correct value part.
You could do pluck('name','name'); to get the values you want into the form.
If you look at the the documentation of the method pluck you see that the second parameter is for the 'key' part of the array that will be returned.
I have a form, inside I have a select with some options and I'm using Laravel Collective Forms to build it, I have something like:
{!! Form::select('size', $data, $selecteds, ['multiple' => true]) !!}
All going well until here, but now I need to set a data-section attribute to each option, how can I make it?
I had to do the same thing recently. After inspecting the FormBuilder class to write my own marco I found out that the select() method actually has an undocumented fifth parameter for option attributes:
Form::select('size', $data, $selecteds, ['multiple' => true], $optionAttributes)
The index must match the value of the option, e.g.:
$optionAttributes = [
'S' => [
'data-title' => 'Small',
'data-surcharge' => '0',
],
'M' => [
'data-title' => 'Medium',
'data-surcharge' => '5',
],
'L' => [
'data-title' => 'Large',
'data-surcharge' => '10',
],
];
So I ended up writing a marco which generates this array based on a collection and then uses the default select() method. Something like that:
\Form::macro('locationSelect', function ($name, $value = null, $attributes = []) {
// Get all locations from the DB
$locations = \App\Location::all();
// Make an id=>title Array for the <option>s
$list = $locations->pluck('title', 'id')->toArray();
// Generate all data-attributes per option
$optionAttributes = [];
foreach ($locations as $location) {
$optionAttributes[$location->id] = [
'data-icon' => $location->icon,
'data-something' => $location->some_attribute,
];
}
// Use default select() method of the FormBuilder
return $this->select($name, $list, $value, $attributes, $optionAttributes);
});
Very convenient.
{{ Form::locationSelect('location_id') }}
You can pass option attributes as fifth parameter (version 5.8) like this
$optionParameters = collect($optionsArray)->mapWithKeys(function ($item) {
return [$item[id] => ['data-anything' => $item['anything']]];
})->all();
And select will look like
{!! Form::select('name', $optionsArray, null, ['class' => 'form-control', 'placeholder' => 'Select'], $optionParameters) !!}
I think it is much simpler and cleaner than creating macroses
Add it to a 4th argument which is an array:
{!! Form::select('size', $data, $selecteds, ['data-attribute' => 'John Smith', 'multiple' => true]) !!}
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');