Laravel:提交时,表单选择选项值始终为NULL

时间:2014-08-07 23:57:27

标签: php forms select laravel option

我有一个表格,我把我的乐队表的de id放在一个选择选项中。当我在下拉列表中选择一个id并尝试提交表单时,该值始终为NULL。如何检索我选择的值?

create.blade.php文件:

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

我的商店行动中的我的Bandcontroller:

$bands->band_id = $input['band_id'];

2 个答案:

答案 0 :(得分:0)

首先,您使用@foreach ($band as $band),确保它不是拼写错误,如果没有,则修复它(可能是$bands as $band并假设您知道我的意思)。无论如何,你也可以试试这个:

{{ Form::select('band_id', $bands, Input::old('band_id')) }}

只需将$bands变量传递给ViewLaravel即可为您创建SELECT,您无需手动循环。要检索该值,您可以尝试:

$band_id = Input::get('band_id');

答案 1 :(得分:0)

Laravel 6或更高版本

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

    }