doushajian2018 2017-01-22 21:28
浏览 94

select选项中的值在laravel中返回NULL

I am trying to insert a record into the database using an id from a dropdown. I am then trying to pass it to my model and save the record, however it says that the hero_id is null, even though the request should pick up the hero_id from the value of the dropdown.

My view:

<form method='POST' action='/profile/addPlay'>            
        <div class="form-group col-xs-12">
            <label for="title">Title</label>
            <input type="text" value="{{old('title') }}" name="title" class="form-control" aria-describedby="emailHelp" >
        </div>
        <div class="form-group col-xs-12">
            <label for="video">Video</label>
            <input type="text" value="{{old('video') }}" name="video" class="form-control" >
        </div>
        <div class="form-group col-xs-12">
                <label for="hero_id">Hero</label>
                <select name="hero_id" class="form-control" id="hero_id">
                    @foreach ($hero as $heroes)
                        <option value="{{$heroes->id}}">{{$heroes->name}}</option>
                    @endforeach
                </select>
            </div>
        <div class="form-group col-xs-12">
            <button type="submit" class="btn btn-primary">
                Add your play
            </button>
        </div>
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </form>
    @if(count($errors))
        @foreach ($errors->all() as $error)
            <li>{{$error}}</li>
        @endforeach
    @endif

My controller:

public function insertPlayProfile (Request $request,Hero $hero){

    $this->validate(
        $request,[
            'title' => 'required|min:3',
            'video' => 'required'
        ]
    );

    $play = new Plays($request->all());


    $hero->addPlayOnProfile($play, Auth::user()->id);

    return back();
}

My model:

public function addPlayOnProfile(Plays $play, $userId){
        $play->user_id = $userId;
        return $this->plays()->save($play);
    }

I have also made the user_id fillable in my Plays model.

I am getting this error:

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: plays.hero_id (SQL: insert into "plays" ("title", "video", "user_id", "hero_id", "updated_at", "created_at") values (thghfghgf, ghgfdhfg, 1, , 2017-01-22 21:23:56, 2017-01-22 21:23:56))

Any suggestions?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 单纯型python实现编译报错
    • ¥15 c++2013读写oracle
    • ¥15 c++ gmssl sm2验签demo
    • ¥15 关于模的完全剩余系(关键词-数学方法)
    • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
    • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
    • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
    • ¥15 安装quartus II18.1时弹出此error,怎么解决?
    • ¥15 keil官网下载psn序列号在哪
    • ¥15 想用adb命令做一个通话软件,播放录音