Reputation: 2387
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'];
Upvotes: 1
Views: 4528
Reputation: 11485
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',
]);
}
Upvotes: 0
Reputation: 146221
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');
Upvotes: 0