【问题标题】:Laravel Too few arguments in function Model::destroy()Laravel 函数 Model::destroy() 中的参数太少
【发布时间】:2021-02-21 07:24:49
【问题描述】:

我正在像这样在 Laravel 8.6.0 中设置删除路由:

api.php

Route::delete('code-rule/{id}', 'api\v1\CodeRuleController@destroy');

CodeRuleController.php

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CodeRule  $codeRule
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return response()->json(CodeRule::where('id', $id)->first()->destroy());
    }

现在,当我在邮递员中尝试向 url localhost:8080/api/v1/code-rule/13/ 发送 delete 请求时,我得到以下响应:

ArgumentCountError: Too few arguments to function Illuminate\Database\Eloquent\Model::destroy(), 
0 passed in C:\Ontwikkeling\TenT en Batchcontrol\API\app\Http\Controllers\api\v1\CodeRuleController.php
on line 112 and exactly 1 expected in file 
C:\Ontwikkeling\TenT en Batchcontrol\API\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php 
on line 905

我不知道为什么会发生这种情况,当我用谷歌搜索时,我只会让人们想要传递更多的论点,而不是我面临的问题。

【问题讨论】:

  • 我想,如果你把destroy()改成delete()就行了

标签: php laravel laravel-routing laravel-8


【解决方案1】:

destroy() 方法接受参数destroy($primaryKey),用于删除模型实例,例如:

CodeRule::destroy(1);
CodeRule::destroy(1, 2, 3);

您可以像这样使用destroy() 方法:

CodeRule::destroy($id);

或者您可以使用 delete() 方法代替:

CodeRule::where('id', $id)->first()->delete();

destroy() 方法单独加载每个模型并在它们上调用delete() 方法,以便触发deletingdeleted 事件。

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 2021-05-14
    • 2019-03-05
    • 2018-01-07
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多