https://dev.to/shanisingh03 #make-rest-api-in-laravel-9 Make REST API in LARAVEL 9 In this video i am going to explain about CRUD Operation using REST API.

* * @return void */ public function down() { Schema::dropIfExists('posts'); }}; Next update the Model fillable property in app/models/Post.php json([ 'status' => true, 'posts' => $posts ]); }

* * @param \Illuminate\Http\Request $request * @param \App\Models\Post $post * @return \Illuminate\Http\Response */ public function update(StorePostRequest $request, Post $post) { $post->update($request->all()); return response()->json([ 'status' => true, 'message' => "Post Updated successfully!", 'post' => $post ], 200); }

/** * Login The User * @param Request $request * @return User */ public function loginUser(Request $request) { try { $validateUser = Validator::make($request->all(), [ 'email' => 'required|email', 'password' => 'required' ]); if($validateUser->fails()){ return response()->json([ 'status' => false, 'message' => 'validation error', 'errors' => $validateUser->errors()], 401); } if(!Auth::attempt($request->only(['email', 'password']))){ return response()->json([ 'status' => false, 'message' => 'Email & Password does not match with our record.', ], 401); } $user = User::where('email', $request->email)->first(); return response()->json([ 'status' => true, 'message' => 'User Logged In Successfully', 'token' => $user->createToken("API TOKEN")->plainTextToken ], 200); } catch (\Throwable $th) { return response()->json([ 'status' => false, 'message' => $th->getMessage()], 500); }} } #protect-api-with-authentication-we-need-to-use-raw-authsanctum-endraw-middleware Protect API With Authentication we need to use auth:sanctum middleware.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners