[英]Laravel API Rest
給我一個錯誤
“調用成員函數delete()時為null”
請幫忙
我在web.php中的路由定義是`
路線:: resource('posts','postcarController'); 我的控制器代碼如下
namespace App\\Http\\Controllers; use Illuminate\\Http\\Request; use App\\PostModell; use Illuminate\\Support\\Facades\\Auth; class postcarController extends Controller { public function __construct() { $this->middleware('auth'); } public function destroy($id) { //PostModel::where('id',$id)->delete(); $post = PostModel::find($id); if($post!=null){ $post->delete(); return response()->json([ 'message' => 'book deleted', ], 200); } } }
`我將網址適當更改為http://127.0.0.1:8000/posts/3並收到錯誤消息
該頁面因不活動而過期。
請刷新,然后重試。
與其fetching
記錄然后將其deleting
,不如使用一行-
PostModel::where('id',$id)->delete();
或者,您可以檢查記錄是否存在該ID,然后刪除它-
$post = PostModel::find($id);
if($post!=null){
$post->delete();
return //your new response;
}
return //response with message that no record exits
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.