Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel POST Method returning Status: 405 Method Not Allowed on a POST method

Please find the info below:

NoteController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\NoteRequest;
use App\Models\Note;
use Illuminate\Http\JsonResponse;

class NoteController extends Controller
{
    public function index():JsonResponse
    {
        $notes = Note::all();
        return response()->json($notes, 200);
    }

    public function store(NoteRequest $request):JsonResponse
    {
        $note = Note::create( $request->all() );
        return response()->json([
            'success' => true,
            'data' => $note
        ], 201);
    }

    public function show($id):JsonResponse
    {
        $note = Note::find($id);
        return response()->json($note, 200);
    }

    public function update(NoteRequest $request, $id):JsonResponse
    {
        $note = Note::find($id);
        $note->update($request->all());
        return response()->json([
            'success' => true,
            'data' => $note,
        ], 200);
    }

    public function destroy($id):JsonResponse
    {
        Note::find($id)->delete();
        return response()->json([
            'success' => true
        ], 200);
    }
}

NoteRequest.php

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class NoteRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title', 'required|max:255|min:3',
            'content', 'nullable|max:255|min:10',
        ];
    }
}

Note.php (model)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
    use HasFactory;

    protected $guarded = [];
}

api.php

<?php

use App\Http\Controllers\NoteController;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->group(function () {
    Route::resource('/note', NoteController::class);
});

php artisan route:list

GET|HEAD        / ......................................................................................................................  
  POST            _ignition/execute-solution ............... ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionController  
  GET|HEAD        _ignition/health-check ........................... ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController  
  POST            _ignition/update-config ........................ ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController  
  GET|HEAD        api/v1/note .......................................................................... note.index › NoteController@index  
  POST            api/v1/note .......................................................................... note.store › NoteController@store  
  GET|HEAD        api/v1/note/create ................................................................. note.create › NoteController@create  
  GET|HEAD        api/v1/note/{note} ..................................................................... note.show › NoteController@show  
  PUT|PATCH       api/v1/note/{note} ................................................................. note.update › NoteController@update  
  DELETE          api/v1/note/{note} ............................................................... note.destroy › NoteController@destroy  
  GET|HEAD        api/v1/note/{note}/edit ................................................................ note.edit › NoteController@edit  
  GET|HEAD        sanctum/csrf-cookie .................................. sanctum.csrf-cookie › Laravel\Sanctum › CsrfCookieController@show

thunder request (same as postman)
Thunder Client

JSON Request

{
  "title": "Hello World",
  "content": "Lorem ipsum."
}

Trying to make a JSON POST REQUEST and getting Status: 405 Method Not Allowed and I’m using php artisan serve, I could provide GIT project if necessary. Please let me know.

>Solution :

Your validation rules look incorrect. In your NoteRequest class, the rules should be an associative array where the keys are the field names and the values are the validation rules. However, in your code, the rules are defined as a comma-separated list of strings. This might cause the validation to fail and return a 405 Method Not Allowed error.

public function rules()
{
    return [
        'title' => 'required|max:255|min:3',
        'content' => 'nullable|max:255|min:10',
    ];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading