【问题标题】:Laravel, data update Error. There is no error reminder, but the data does not insert into databaseLaravel,数据更新错误。没有错误提示,但是数据没有插入数据库
【发布时间】:2020-08-23 02:03:15
【问题描述】:

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

我有一个主管表,其中包括 id、user_id、name、email、专业知识和首字母列。主键是 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();

    }

【问题讨论】:

  • 视图中的$supervisor 是什么,为什么要迭代它?
  • 主管是数据库中的主管列表

标签: php mysql laravel


【解决方案1】:

我认为问题造成的

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)}}"

【讨论】:

  • 谢谢你,兄弟。但它不适用于我的代码。我对此一无所知。伤心
  • 您能否在验证数据之前检查 supProfileUpdate 函数中 $supervisor 的值是多少
猜你喜欢
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 2011-02-10
  • 2011-06-22
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多