Get the id of a selected value in Selectbox Laravel 5.1 - php

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>

Related

Laravel Passing Variables to Multiple Select Drop Down Array to string conversion Error

I am trying to push multiple selections from a drop down of specialties being displayed from my $specialties variable in an #foreach in a laravel application. I am able to capture 1 id and push it to the database, but multiple selections do not work. It only pushes one ID to the database. When I try name="specialties[]" as below I get error: Array to string conversion. How do I push my selection into an array using eloquent?
My form View:
<div class="container">
{{ csrf_field() }}
<strong>Select Specialty:</strong>
<select id="multiple-checkboxes2" multiple="multiple" name="specialties[]" value="specialties">
#if($specialties)
#foreach($specialties as $specialty)
<option value=" {{$specialty->id}} ">{{$specialty->name}}</option>
#endforeach
#endif
</div>
</select>
{!! Form::close() !!}
<script type="text/javascript">
$(document).ready(function() {
$('#multiple-checkboxes2').multiselect();
});
Client Store Controller
public function store(Request $request)
{
//
Client::create([
'specialties' => $request->specialties,
] );
return redirect('/accounts');
}
Client display controller
public function display($id)
{
$specialties=Specialty::select( 'name', 'id')->orderBy('name', 'asc')->get('id');
return view('accounts/display', compact('accounts', 'specialties', )->withAccount($accounts);
}
}
try to use array
<select id="multiple-checkboxes2" multiple="multiple" name="specialties[]">
#if($specialties)
#foreach($specialties as $specialty)
<option value= "{{ $specialty->id }}">{{$specialty->name}}</option>
#endforeach
#endif
</select>

Getting data out of array separately in laravel

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>

laravel 5.4 form dropdown from database

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 )) }}

List in Laravel with more than 2 column

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>

Laravel : Form select option value is always NULL when submitting

I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}

Categories