am trying to make old values which would be used when am editing but it's returning
htmlspecialchars(): Argument #1 ($string) must be of type string, array giveneverytime
but when i dd the variable i found that it has array but when it reaches the option it changes into error
this is my select in blade
{!! Form::label('functional_area_id', 'Functional Area', ['class' => 'bold']) !!}
<?php
$functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds);
?>
{{-- this codes works perfect but it doesn't display the relation between child and parent and i want that to be displayed
{!! Form::select('functional_area_id[]', $functionalAreas, $functionalAreaIds, array('class'=>'form-control select2-multiple', 'id'=>'functional_area_id','multiple'=>'multiple')) !!} --}}
<select name="functional_area_id[]" id="functional_area_id" class="form-control select2-multiple" multiple="multiple">
#foreach ($menulist as $category)
<option value={{ $category->functional_area_id }} {{ $functionalAreaIds }} >{{ $category->functional_area }}</option>
#if (count($category->children) > 0)
#include('submenu', ['submenu' => $category->children, 'parent' =>
$category->functional_area])
#endif
#endforeach
</select>
{!! APFrmErrHelp::showErrors($errors, 'functional_area_id') !!}
</div>
this is the controller which fetches the array
public function jobFunctionals()
{
return $this->hasMany('App\JobFunctionalAreas', 'job_id', 'id');
}
public function getJobFunctionalsArray()
{
return $this->jobFunctionals->pluck('functional_area_id')->toArray();
}
You can only output strings using {{ $variable }}, because Blade will run $variable through htmlspecialchars escaping, that's why you get an error trying to output {{ $functionalAreaIds }} which, judging by the name, is an array.
I have no idea what you are including with the submenu partial, but it is likely invalid HTML. The only tags that are allowed within a select element are option and optgroup tags. Optgroups need to be opened and closed around options, so I don't see how this would work in your setup.
Also, you have syntax errors in your option tag:
<option value={{ $category->functional_area_id }} {{ $functionalAreaIds }} >{{ $category->functional_area }}</option>
What do you expect this to do? This would - if it worked at all - result in something like this:
<option value=1 [1,5,6] >Area 1</option>
Which is clearly not valid HTML. What do you expect the array to do after every option value?
To fix the option part of your code, you can do something like this:
<option value="{{ $category->functional_area_id }}" #if(in_array(category->functional_area_id, $functionalAreaIds)) selected #endif >{{ $category->functional_area }}</option>
This will set all options selected that are within the $functionalAreaIds array.
Maybe you should get rid of the code including the submenu when testing, because this is likely to break your HTML too, and then take it from there.
Another tip:
<?php
$functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds);
?>
can be written in blade like this:
#php($functionalAreaIds = old('functional_area_id',$jobFunctionalAreasIds))
Related
I'm using shipping system where will calculate shipping price.
this is what i get in my blade currently:
As you see I get id of state and then name of state.
here is my function:
public function index() {
$data = RajaOngkir::Provinsi()->all();
return view('welcome', compact('data'));
}
and this is my blade code:
#foreach($data as $sss)
#foreach($sss as $numbers)
{{ $numbers }} <br>
#endforeach
#endforeach
here is {{$data}} dump info:
what I want is to getting id and name separately so I can use it
in input.
The foreach will loop through anyhow and pull the relevant info. You just then show it within your blade like so:
#foreach ($data as $info)
ID: {{ $info->province_id }} <br />
Name: {{ $info->province }}
#endforeach
Updated:
From my re read you want it as a form drop down select so you can calculate shipping fee's therefore you'd simply do:
<select name="province" id="">
<option value="">Select</option> -> This will be the default select option
#foreach ($data as $info)
<option value="{{ $info->province_id }}">{{ $info->province }}</option>
#endforeach
</select>
What I am trying to do is basicly passing selected item's slug from a select box to route
{{ Form::open(['route' => 'list/{category->slug(here)}' ]) }}
<select name="" id="" class="form-control">
#foreach($categories $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
{{ Form::submit('Send') }}
{{ Form::close() }}
Can anyone help me?
You need to use some javascript (jQuery) to do this
$("#select").change(function() {
var option = $(this).val();
$("#form-id").attr("action", "list/" + option);
});
You can try to use the onchange. Like here:
<select onchange="document.location=this.options[this.selectedIndex].value">
<option value="{{ route('route_name') }}">caption</option>
I want to to use foreach loop in select form. But I dont figure out how can I do that.
I searched about it in here, in other questions and found lists method. But when I tried it retuns me "Call to undefined method App\Category::lists()" error.
{{ Form::select('categories',
"foreach loop" for bringing categories
) }}
Any advice ?
As discribed in the Laravel Recipes :
If you use this :
{{ Form::select('age', ['Under 18', '19 to 30', 'Over 30']) }}
You will get this output :
<select name="age">
<option value="0">Under 18</option>
<option value="1">19 to 30</option>
<option value="2">Over 30</option>
</select>
So in your case you can use it like this :
{{ Form::select('categories', $categories->pluck('name')) }}
If you want to addthe id of the category as value of the option you can do it like this :
In the controller :
$categories = [''=>''] + Category::lists('name', 'id')->all();
return view('back.create')->withCategories($categories);
And in the view :
{{ Form::select('categories', $categories) }}
I want to fetch drop down option value & name from database, to do that my controller code is
$roles = Role::pluck('role','user_id');
return view('users.add_role',compact('roles'));
to fetch this data from drop down list my view file code is
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role->user_id }} "> {{ $role->role }} </option>
#endforeach
</select>
but it says error
Trying to get property of non-object (View: C:\xampp\htdocs\auth2\resources\views\users\add_role.blade.php)
if i just only use
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role }} "> {{ $role }} </option>
#endforeach
</select>
then it shows role column of the table, but it not pass option value to database. so what is the correct way to generate option value & name from database?
Your $roles variable containt an array which is looks like
[0] => 'your_role'
[1] => 'your_role'
Where is 0 and 1 index is user_id. So your user_id is set as an index of your $roles array. Simply do it dd($roles) and check the output. $key as $role will work. Where $key will containt the user_id.
#foreach($roles as $key => $role)
<option value="{{ $key }} "> {{ $role }} </option>
#endforeach
I use laravel collective package to generate dropdown list much more easier,
you can do things like, on your blade template and it will render the list for you
{{ Form::select('role', $role )) }}
Using Laravel 5, I'm trying to make a list of countries in a dropdown.
I use this syntax :
$countries= Countries::get('name', 'id');
And then, with blade
{!! Form::select('countries', $countries) !!}
This gives me a nice list, but I would like to set a custom attribute on every country. This argument is the continent of the country, something like "data-continent='X'".
This continent is a column of my table "countries".
Add the continent column to your query
$countries= Countries::get('name', 'id', 'continent');
Then loop over the results and ouput using any format you prefer.
<select name="country">
#foreach ($countries as $c)
<option data-continent="{{ $c->continent }}" value="{{ $c->id }}">{{ $c->name }}</option>
#endforeach
</select>