Search code examples
apirouteslaravel-routinglaravel-9

Laravel 9 error route | The GET method is not supported


I am having a problem fetching a list of certain customers (authenticated users) via API. When I use this route in Postman I receive the following error.

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file D:\api\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 118

api.php

Route::post('/register', [UserAuthController::class, 'register']);

Route::post('/login', [UserAuthController::class, 'login'])
    ->name('login');

Route::apiResource('/customer', CustomerController::class)
    ->middleware('auth:api');

Controller

class CustomerController extends Controller
{
    public function index()
    {
        $customers = Customer::all();

        return response([ 'customers' =>
            CustomerResource::collection($customers),
            'message' => 'Successful'], 200);
    }

    public function store(Request $request)
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'first_name'=>'required|max:120',
            'email'=>'required|email|unique:users',
            'password'=>'required|min:6'
        ]);

        if($validator->fails()){
            return response(['error' => $validator->errors(), 
                'Validation Error']);
        }

        $customer = Customer::create($data);

        return response([ 'customer' => new CustomerResource($customer),
            'message' => 'Success'], 200);
    }

    public function show(Customer $customer)
    {
        return response([ 'customer' => new CustomerResource($customer),
            'message' => 'Success'], 200);
    }
    
    public function update(Request $request, Customer $customer)
    {
        $customer->update($request->all());

        return response([ 'employee' => new CustomerResource($customer), 
            'message' => 'Success'], 200);
    }
    
    public function destroy(Customer $customer)
    {
        $customer->delete();

        return response(['message' => 'Customer deleted']);
    }
}

Solution

  • I solved this problem by adding Accept|json/application in headers of Postman.