Courses

How to Build Laravel 11 API From Scratch

Create a New Record: POST from HTML Form

Summary of this lesson:
- Implementing POST endpoint for creating categories
- Returning newly created record in API response
- Handling POST requests from Vue.js frontend

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...

The full lesson is only for Premium Members.
Want to access all 23 lessons of this course? (58 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord