Hello Friends 👋,

Welcome To Infinitbility! ❤️

This article will help you to solve your 405 error on your laravel project with explanation of 405 Method Not Allowed like why you are getting this error and how to solve it.

Let’s start today’s article How to solve Laravel 405 Method Not Allowed

What is 405 Method Not Allowed?

The 405 Method not allowed is an HTTP response status code that indicates that the method received in the request is known by the origin server but not supported by the target resource. There can be different causes of this error but all are related to the way we have defined routes and configured middleware in lumen. A common cause of this error is when we do not access a method in the way it is defined in routes.

why you are getting this error

405 Method Not Allowed means that the HTTP method is simply not supported. For example, a client might do a POST request on a resource where POST is not implemented or it’s meaningless.

A server generating the 405 response must also tell the client which HTTP methods it can do, using the Allow header.

405 http status code and it’s say u calling api in get but url available only in post method

Let’s understand with example

Assume you create post route in Laravel like below


use App\Http\Controllers\UserController;

Route::post(/user', [UserController::class, 'index']);

But u trying to call in get method like below

<form action="/user">
    @csrf
    ...
</form>

But right is you have to define method in form like below

<form method="POST"  action="/user">
    @csrf
    ...
</form>

But when you using Laravel put, patch, and delete you have to define method in laravel style

<form action="/example" method="POST">
    @method('PUT')
    @csrf
</form>

Laravel put, patch, and delete Ajax example


$.ajax({
    url: "/ajax-request",
    type:"POST",
    data:{
        name:name,
        _token: _token,
        _method: "PUT",
    },
    success:function(response){
        console.log(response);
    },
});

Thanks for reading…