Skip to main content

Create a New Record: POST from HTML Form

Premium
5:15

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 of our courses? (34 h 53 min)

You also get:

59 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

M
M ✓ Link copied!

fyi to remove authroization until we set it up, modify:

class StoreCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
// return false;
return true;
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.