1

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'];
1
  • I think you are using Input in a wrong way, look at this Or just use $request variable. Commented Feb 1, 2020 at 18:03

2 Answers 2

0

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');
Sign up to request clarification or add additional context in comments.

1 Comment

It loads all the fields of the table bands in the dropdown at the moment.
0

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',
        ]);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.