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>
Related
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))
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>
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 )) }}
I want to get the id of the selected value or item from a dropdown selectbox because I want to save it in the database. So, I tried passing the it as a value to get it with an hidden field on the page but it does work. How do I do this?
This is the form page:
<p>{!! Form::select('companyname', array('' => 'Select a Company') + $listCompanies) !!} </p>
#foreach($istCompaniesId as $company)
#if(companyname->text === $listCompaniesId->value)
{!! Form::hidden('company_id', $listCompaniesId->value) !!}
#endif
#endforeach
This is the controller:
$listCompanies = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'companyname')->toArray();
$listCompaniesId = Company::where('user_id', '=', Auth::user()->id)->orderBy('companyname', 'desc')->lists('companyname', 'id')->toArray();
return view('product.create')
->with('listCompanies', $listCompanies)
->with('listCompaniesId', $listCompaniesId);
Since you are using Laravel 5.1, I suggest not to use the form helper. You can simply combine the option tag with blade's #foreach.
<select name="company_id">
<option value="">Choose Company</option>
#foreach($listCompanies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
#endforeach
</select>
How can i get a dropdown to select whatever id is present within the database.
I have a user's table that has a location_id and a towns table that has town_id and town_name so I want the dropdown to know what the user table location_id is and show the town_name based on the id.
How can this be done in Laravel?
Thanks in advance.
What you would do is assign your user and towns to the view. Next loop your towns table to generate your select list. Something like:
<select name="location">
#foreach($towns as $town)
<option value="{{ $town->id }}" {{ ($user->location_id == $town_id) ? "selected" : "" }}>{{ $town->town_name }}</option>
#endforeach
</select>
Ok fixed it to match blade:
<select name="location" class="form-control">
# foreach($towns as $town)
<option value="{{ $town->town_id}}" #if ($user->location == $town->town_id) "selected" #else "" #endif>{{ $town->town }}</option>
#endforeach
</select>
But it's not showing the selected item
If you are sending correct data to the view and the problem is only repopulating the select then you may try this, just put this in your view and send $towns from your controller as you are sending now
{{ Form::select('towns', $towns, Input::old('towns')) }}
No need to manually loop through the array. Laravel's Form class will build the select/dropdown.
My last answer works, just find it was cached by the looks in the browser, so all fixed.