【问题标题】:The update nor the destroy methods won't work in laravel eloquent model?更新和销毁方法在 laravel eloquent 模型中不起作用?
【发布时间】:2019-12-23 00:01:34
【问题描述】:

我有一个奇怪的情况,即 eloquent 模型不允许我更新或销毁,而索引和创建工作正常!

我正在使用 Vue.js 和 Laravel API Resource 进行表单控制,虽然它以前对我有用,但在这里不起作用:

这是我的 Vue.js 代码:

updateFinish(finish) {
    axios.patch(`/api/finishes/${finish.id}`, finish).then(response => { 
        this.fetchFinishes();
    }).catch(error => {
      // Get laravel validation error
      this.errors = error.response.data.errors;
   });
},

laravel 更新代码(不工作)

public function update(FinishType $finishType)
    {
        // Don't know why not working
        $finishType->update($this->validateRequest());
        return new FinishTypeResource($finishType);
    }

回复是null{"id":null,"name":null}

虽然此代码有效:

public function update($id)
    {
        $finishType = FinishType::find($id);
        $validates = $this->validateRequest();
        $finishType->name = $validates['name'];
        $finishType->save();
        return new FinishTypeResource($finishType);
    }

public function validateRequest()
    {
        return request()->validate([
            'name' => 'required | unique:finish_types',
        ]);
    }

注意模型名称是FinishType,数据库表名称是finish_types,我什至尝试像protected $table = 'finish_types'那样在模型中定义表名称——仍然无法正常工作,我已经定义了$fillable数组!!!

【问题讨论】:

  • 请出示validateRequest()函数的代码,我在laravel源码中没有找到。
  • 是的,抱歉,我确实编辑了帖子以包含 validateRequest() 函数...问题是我对其他模型有相同的确切代码并且它正在工作查找!

标签: laravel vue.js eloquent model


【解决方案1】:

您的路由模型绑定无法正常工作,隐式绑定 工作您的injected variable 应该匹配route parameter name

假设您的参数名称可能是 finish(从您的 javascript 中读取 url),您必须使用 $finish 作为注入变量来编写更新函数,如下所示:

public function update(FinishType $finish)
{
    $finish->update($this->validateRequest());
    return new FinishTypeResource($finish);
}

对 destroy() 执行相同操作:

public function destroy(FinishType $finish)
{
     // your destroy code here
}

在任何情况下,您都可以运行php artisan route:list 来找到您的parameter name(大括号中的URI 部分)并将同名 赋予injected variable

如果两者不匹配,参数和注入的变量名,laravel 会注入一个 void,未加载,FinishType 模型,因此对其进行更新或删除是没有意义的。

【讨论】:

  • 这是我的 API 路由 // Finishes API Route::get('/finishes', 'FinishTypeController@index'); Route::post('/finishes', 'FinishTypeController@store'); Route::patch('/finishes/{finish}', 'FinishTypeController@update'); Route::delete('/finishes/{finish}', 'FinishTypeController@destroy');
  • 所以我的回答是有效的:你必须使用$finish 作为注入变量,因为你使用{finish} 作为参数名称
  • 好吧,我确实将路由重命名为这个,它工作得很好,谢谢Route::patch('/finishtypes/{finishType}', 'FinishTypeController@update');
【解决方案2】:

我无法发布 cmets,所以我将发布我认为是答案的内容。

当路由 url 名称对应于我认为的表的名称...或模型时,Laravel 会自动执行路由模型绑定。

所以users/{id} 会在您将用户对象作为控制器中的参数键入时自动绑定它。示例(User $user)

但是,由于您的 URL 似乎与您的模型/表的名称“不同”,请转到 RouteServiceProvider,并手动进行绑定。

所以在你的情况下,你会在 RouteServiceProvider 类的引导函数中做这样的事情:

public function boot()
{
    parent::boot();

    Route::model('finishes', FinishType::class);
}

不要忘记你的进口 :)

您可以在此处阅读有关显式模型绑定的更多信息:https://laravel.com/docs/5.8/routing#explicit-binding

【讨论】:

  • 这是我的 API 路由 // Finishes API Route::get('/finishes', 'FinishTypeController@index'); Route::post('/finishes', 'FinishTypeController@store'); Route::patch('/finishes/{finish}', 'FinishTypeController@update'); Route::delete('/finishes/{finish}', 'FinishTypeController@destroy');
  • 是的。您需要在 RouteServiceProvider 文件中指定绑定,因为 finishes 不会自动绑定 FinishType::class... 您的问题可以通过手动执行我上面指定的单行绑定来解决,或者将您的网址切换为 /finishTypes/
  • @Keroles 做我上面提到的。找到 RouteServiceProvider 文件并将 Route::model('finishes', FinishType::class); 行添加到引导方法中。不要忘记在类的顶部添加 FinishType 模型的导入。
  • 好吧,我确实将路由重命名为这个,它工作得很好,谢谢Route::patch('/finishtypes/{finishType}', 'FinishTypeController@update');
猜你喜欢
  • 2018-09-06
  • 2021-02-22
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 2021-10-13
  • 2019-01-26
  • 1970-01-01
  • 2021-11-25
相关资源
最近更新 更多