Until now, we have been talking about API returning data from the server. Now, it's time to post data. We will create a new category.
For Controller methods, we will use the usual naming.
routes/api.php:
Route::get('/user', function (Request $request) { return $request->user();})->middleware('auth:sanctum'); Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index']);Route::get('categories/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'show']);Route::post('categories', [\App\Http\Controllers\Api\CategoryController::class, 'store']); Route::get('products', [\App\Http\Controllers\Api\ProductController::class, 'index']);
We will create a new category in the Controller, but what will we return? In a regular form without API, it would usually return a redirect to some page. But in this case, we must return...