1

I'm trying to validate the request in my API but when I use the validate method or even create a class that extends FormRequest, it gives me the 405 error. Below you can see my controller and the way routes are defined. It only gives me 405 error when the validation fails, I assume, maybe, somehow there is a callback function sending GET method request but I have no clue how to fix it.

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Interfaces\PostRepositoryInterface;

class PostController extends Controller
{

    private $postRepository;

    public function __construct(PostRepositoryInterface $postRepository)
    {
        $this->postRepository = $postRepository;
    }


    public function index()
    {
        return $this->postRepository->all();
    }

    public function store()
    {

        request()->validate([
            'title'=>'required|min:2',
        ]); 

        return $this->postRepository->createPost();
    }

    public function show($postId)
    {
        return $this->postRepository->findById($postId);
    }

}

Error message:

Route::group(['middleware' => 'auth:api', 'namespace'=> 'API'], function(){


    Route::get('post'            , 'PostController@index'  );
    Route::post('post'           , 'PostController@store'  );
    Route::get('post/{post}'     , 'PostController@show'   );
    Route::get('post/{post}/edit', 'PostController@edit'   );
    Route::post('post/{post}'    , 'PostController@update' );
    Route::post('post/{post}'    , 'PostController@destroy');

});
3
  • Please can you show the form that is making the request? Also, if you're using an javascript with it please can you show that too. Commented Dec 28, 2019 at 22:04
  • I'm using Insomnia software to simulate a request. Commented Dec 28, 2019 at 22:06
  • ... the point of the comment was we cannot see what request you are using. Until you've shown us otherwise, the obvious answer that we will assume is that you are doing exactly what the error says - sending a request for which you have no route configured, either wrong method or wrong URL. So, once again - either show us the code that makes the request, or describe (exactly) the request you are creating with whatever Insomnia is. Eg the screenshot seems to show it was generated on your home page, at / - there is no route shown for that in your code. Commented Dec 29, 2019 at 10:43

1 Answer 1

1

Thanks everybody that tried to help, i found the solution in this post

MethodNotAllowedHttpException using Form Request Validation on Laravel 5.5

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.