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',
]);
}
Related
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>
I'm currently using Laravel and I want to have a dropdown box display category names from my database, but when selected the value it returns to the server should be a numerical value of the name selected which is also stored in the database.
Like so:
#foreach($categories as $category)
<option name="pid" value="$category->id" id="cname">{{$category->name}}</option>
#endforeach
The values are passed from my Controller to the Blade template. As you can see, I want it to display the category names in the drop down (which it does fine), but once selected and submitted, I want the category ID to be returned. Since the "value" attribute returns the value, I want to have the variable $category->id as the "value". I want to do this without using JavaScript or jQuery. Only with PHP. How would I go about accomplishing this and is it even possible?
When I try to use value="{{$category->id}}" I get the Laravel error:
Integrity constraint violation: 1048 Column 'parent_id' cannot be null
So it seems like it isn't submitting the value of the dropdown.
Here is the code I'm currently using in my Blade template:
<form class="" action="{{route('createsubcategorypost')}}" method="post">
<div class="form-group">
<label for="cname">Sub-Category name:</label>
<input type="text" name="cname" value="" placeholder="Sub-Category Name" id="cname" class="form-control">
</div>
<div class="form-group">
<label for="pid">Parent ID</label>
<select class="form-control">
#foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="pid">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<center>
<button type="submit" name="button" class="btn btn-success">Create Sub-Category</button>
</center>
</div>
{{csrf_field()}}
</form>
And this is the code in my Controller:
public function CreateSubCategoryPost(Request $request){
if ($request->cname == null) {
session()->flash('errormessage',' Invalid sub-category name.');
return redirect()->back();
}
$scc = SubCategory::where('name',$request->cname)->first();
if ($scc !== null) {
session()->flash('errormessage',' Sub-category with that name already exists.');
return redirect()->back();
}
$subCategory = new SubCategory;
$subCategory->name = $request->cname;
$subCategory->uniqueid = 'SCA'.str_random(28);
$subCategory->slug = str_slug($request->cname,'-');
$subCategory->parent_id = $request->pid;
$subCategory->save();
return redirect()->route('categories');
}
Same way you you are displaying the category name inside the option tag :
#foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="cname">{{$category->name}}</option>
#endforeach
Everything you call from a database in Laravel should have the format {{$category->someName}} no matter where you place it in your view. In your case you need to enclose $category->id in {{ }}
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 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>