Php 函数模型::destroy()中的参数太少
我正在Laravel 8.6.0中设置删除路由,如下所示: api.phpPhp 函数模型::destroy()中的参数太少,php,laravel,laravel-routing,laravel-8,Php,Laravel,Laravel Routing,Laravel 8,我正在Laravel 8.6.0中设置删除路由,如下所示: api.php Route::delete('code-rule/{id}', 'api\v1\CodeRuleController@destroy'); coderRuleController.php /** * Remove the specified resource from storage. * * @param \App\Models\CodeRule $codeRule
Route::delete('code-rule/{id}', 'api\v1\CodeRuleController@destroy');
coderRuleController.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());
}
现在,当我尝试在postman中向urllocalhost: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($primaryKey)
,以删除模型实例,如:
CodeRule::destroy(1);
CodeRule::destroy(1, 2, 3);
您可以这样使用destroy()
方法:
CodeRule::destroy($id);
或者您可以使用方法:
CodeRule::where('id', $id)->first()->delete();
destroy()
方法分别加载每个模型,并对其调用delete()
方法,以便触发deleting
和deleted
事件。我认为,如果将destroy()
更改为delete()
@sta谢谢,这是有效的。