I am new to Laravel and I am using Laravel as an API for my Angular project.
I have no problem when I am using the Laravel upon retrieving data using the get
method but I have a big problem when inserting data into it using post
method
I have the following error:
SymfonyComponentHttpKernelExceptionMethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
I swear this is my code at the api.php
<?php
use AppModelssample;
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpResourcesSampleResource;
use AppHttpControllersSampleController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::match(['get', 'post'], '/', function () {
//
});
//I tried to add this hoping it will work, it wont
Route::post('/sample', [SampleController::class, 'store']);
// Route::post('/sample', function(Request $request){
// return sample::create($request->all);
// });
I have tried following the instructions in the link below hoping it would help me it wouldn’t
this
this
2
Answers
Okay so here is the answer guys, this error is not an error at all after all. IF this oh so called "error" appear on my screen it only means that my API or routes are working I tried to call this one into my angular project and it worked!
SOMETIMES THIS IS NOT ERROR THANK YOU ALL FOR YOUR TIME
As per your problem, The GET method is not supported for this route and your back-end route is the post so when you hit this API you may not use the post method. You must need to use the post method for sending data to the backend. You can follow the below example in jquery but you have to apply it in your angular project context like Axios or anything else:
Client-side request =>
backend route =>
See here I used ajax request type POST and route method type post.