Courses

How to Build Laravel 12 API From Scratch

Create a New Record: POST from HTML Form

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Implementing POST endpoint for creating categories
- Returning newly created record in API response
- Handling POST requests from Vue.js frontend

Video Version of the Lesson

[Only for premium members]

Link to the repository

[Only for premium members]

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 28 video+text lessons of this course? (1 h 21 min)

You also get:

  • 83 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord