Laravel,数据更新错误。没有错误提示,但数据不会插入数据库

时间:2020-08-23 02:03:15

标签: php mysql laravel

我遇到一个问题,即数据无法存储在数据库中。当我使用保存功能更新列时,没有错误提示,也不会插入。我不知道。兄弟,救救我

我有一个主管表,其中包括id,user_id,名称,电子邮件,专业知识和首字母列。主键是user_id。

这是我进行编辑视图的路线

Route::get('supervisor/profile/{id}/edit','UserController@supProfileEdit');

这是发布新数据的视图。

Route::post('supervisor/profile/{id}','UserController@supProfileUpdate');

刀片是一种形式:

 <div class="card">
        <div class="card-header bg-primary text-white">Profile</div>
        <div class="card-body">
            @foreach($supervisor as $data)
                <form id="supProfileForm" type="POST" action="{{url('supervisor/profile',$data->user_id)}}"
                      enctype="multipart/form-data">
                    @csrf
                    <div>
                        <label for="name"> Name : </label>
                        <input  id="name" disabled value="{{$data->name}}">

                    </div>

                    <div>
                        <label for="email">Email : </label>
                        <input id="email"  disabled value="{{$data->email}}">
                    </div>

                    <div>
                        <label class="inline" for="expertise">Expertise : </label>
                        <input type="text" id="expertise" name="expertise" value="{{$data->expertise}}">
                    </div>

                    <div>
                        <button type="submit" class="btn btn-primary">Update</button>
                    </div>
                </form>
        </div>
    </div>
    @endforeach

然后,这是我在Controller中的功能:我只是想更新专业知识。

public function supProfileUpdate(Supervisor $supervisor)
    {

        $this->validate(request(),[
            'expertise'=>'required',
        ]);
        $supervisor->expertise = request('expertise');
        $supervisor->save();
        return redirect()->back();

    }

1 个答案:

答案 0 :(得分:0)

我认为问题是由

引起的
Route::post('supervisor/profile/{id}','UserController@supProfileUpdate');

当您尝试使用user_id密钥获取Supervisor模型主键时,ID是

所以我建议您按照以下方法在Supervisor模型中实现getRouteKeyName

 /**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'user_id';
}

或在将其发送到表单时使用id

<form id="supProfileForm" type="POST" action="{{url('supervisor/profile',$data->id)}}"
相关问题