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();
Related
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']) !!}
652/5000
I am using in my project of Laravel 5 the package https://github.com/spatie/laravel-activitylog in its version 2.3. I have a page where I go through a list of activities and try to get the changes but it returns an empty array.
This is my Controller
public function history($id) {
$incidence = Incidence::find($id);
$activities = Activit::where('subject_id', $incidence->id)->get ();
return view('incidence.history', compact('activities'));
}
This is my html page
#foreach ($activities as $activity)
<p> {{ $activity->created_at }} </p>
<p> {{ $activity->changes() }} </p>
#endforeach
And this is the output in the browser
Clarify that I have done 4 update to the same record which I see reflected in the activity_log table of the database that uses the package. But I do not understand why the arrangement of the changes is not shown as indicated by the site's documentation:
Calling $ activity-> changes will return this array:
[
'attributes' => [
'name' => 'updated name',
'text' => 'Lorum',
],
'old' => [
'name' => 'original name',
'text' => 'Lorum',
],
];
Since it uses a database table I'm certain this has to do with the way Laravel allows methods to be chained. When you do $activity->changes() it expects you to continue the query. IE: $activity->changes()->where('etc', 1)->get() or something similar. So by calling $activity->changes() you are just sending a partial query to eloquent.
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 need advice how to store an array to database. For example i have an input with name="user_phone[]" and i want to store to database the value of this input.
I have a form like so, also there other inputs but i copy just one:
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
and the controller:
public function postAddOrder(Request $request)
{
$this->validate($request, [
'receipt_date' => 'date|required',
'return_date' => 'date|required',
'user_name' => 'required',
'user_phone' => 'required',
'work_sum' => 'integer|required',
'user_descr' => 'required',
'foruser_descr' => 'required'
]);
$user = new User;
$user = $user->all()->find($request->input('user_name'));
$order = new Order([
'receipt_date' => $request->input('receipt_date'),
'return_date' => $request->input('return_date'),
'user_name' => $user->fio,
'user_phone' => $request->input('user_phone'),
'device' => $request->input('device'),
'work_sum' => $request->input('work_sum'),
'master_name' => $request->input('master_name'),
'user_descr' => $request->input('user_descr'),
'foruser_descr' => $request->input('foruser_descr'),
'type' => $request->input('type'),
'status' => $request->input('status'),
'engineer' => $request->input('engineer'),
'partner' => $request->input('partner'),
'office' => $request->input('office')
]);
$order->save();
return redirect()->route('admin.orders.index');
}
The problem is when i'm submitting the form and getting the error:
htmlentities() expects parameter 1 to be string, array given
Also i'm using casts to store an array to DB:
/**
* The attributes that should be casted to native types.
*
* #var array
*/
protected $casts = [
'user_phone' => 'array',
];
The values are storing correctly, the main problem is that the validate() method is not catching the errors. For example im not filling some data in inputs which are required. When instead of getting the error like something is required im getting error
htmlentities() expects parameter 1 to be string, array given
When im filling all input with data everything goes ok.
I think the problem comes from your rule
'user_phone' => 'required
To validate array values you should use the array validation. (Link)
rewrite your rule like so
"user_phone.0" => "required"
this will ensure that at least one user_phone is provided.
In case you wanna validate phone format just go with:
"user_phone.*" => "{Insert validation here}"
Found the definition.
{!! Form::open([route('some.router')]) !!}
<fieldset class="form-group">
{{ Form::label(null, 'Phone') }}
{{ Form::text('user_phone[0]', null, ['class' => 'form-control'] ) }}
</fieldset>
{!! Form::close() !!}
We must pass the index in inputs. Like name="user_phone[0]" after that we are not getting the error:
htmlentities() expects parameter 1 to be string, array given
And validate() method catching the errors. It was the only solution for me.
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');