Home > Blockchain >  The GET method is not supported for this route. Supported methods: POST. But my method is post
The GET method is not supported for this route. Supported methods: POST. But my method is post

Time:09-28

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:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.

I swear this is my code at the api.php

<?php


use App\Models\sample;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Resources\SampleResource;
use App\Http\Controllers\SampleController;


/*
|--------------------------------------------------------------------------
| 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

CodePudding user response:

I do not find any problem in the api.php and your route Route::post('/sample', [SampleController::class, 'store']); seems correct as well.

Please look into your SameController.php if you have written the method 'store' correctly.

And it will be great if you can post the code for 'store' method from your SampleController.php here so that the matter could be further investigated.

Also let us know about the error that you are getting!

CodePudding user response:

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:

Client-side request =>

$.ajax({
        type: "POST",
        url: "{{route('check_inventory_product')}}",
        data: data,
       success: function (response) { 
        console.log(response);
       }
  });

backend route =>

Route::post('check_inventory_product'[InventoryController::class,'check_inventory_product'])->name('check_inventory_product');

See here I used ajax request type POST and route method type post.

  • Related