-1

I'm using postman to test adding a record to database. Function works when all data is sent correctly (using POST method). When I try to to send invalid data in one of the fields postman tells me that

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

Which is odd as POST definitely works when data is correct and record gets added. I am looking to get proper error message and maybe customize it.

After reading answers to similar problems, I added get method for same route as error messaging uses get as I understand? But the error persist even with get method defined.

these are my routes

Route::post('trip', 'TripController@addTrip');
Route::get('trip', 'TripController@errorTrip');

php artisan route:list shows that I have both GET and POST routes for 'trip', error remains

This is my function in laravel

public function addTrip(Request $request) {
    $validated = $request->validate([
        'date' => ['required','regex:/^(2020-08-([0-2][0-9]|30))$/'],
        'user_id'=> ['required','regex:/^[0-9]*$/'],
        'train_id'=> ['required','regex:/^(1|2)$/'],
        'train_route_id'=> ['required', 'regex:/^(1|2)$/'],
        'stop_from'=> ['required','regex:/^[1-8]$/'],
        'stop_to'=> ['required','regex:/^[1-8]$/'],
    ]);
        
    $data = request()->post();

    DB::table('trips')->insert([
        [
            'user_id' => $data['user_id'],
            'train_id' => $data['train_id'],
            'train_route_id' => $data['train_route_id'],
            'stop_from' => $data['stop_from'],
            'stop_to' => $data['stop_to'],
            'date' => $data['date'],
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now()
        ],
    ]);
        
}
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
Jeekim
  • 543
  • 5
  • 15
  • It may be that the URL matches another route, so check other routes to see if this data could match. Not a laravel person so don't know if you can show the route which it is triggering instead. – Nigel Ren Jul 08 '20 at 08:25
  • just try method any instead of post and get – Monika Jul 08 '20 at 08:28
  • 1
    I don't think your code is getting executed after the validate method if you pass in invalid data because the way laravel's validation method works is that, it will redirect to the previous page if the validation fails. Please refer to this [link](https://stackoverflow.com/questions/46350307/disable-request-validation-redirect-in-laravel-5-4) to see how to prevent this behaviour. – Tushar Jul 08 '20 at 08:35
  • 1
    https://laravel.com/docs/7.x/validation mentions that failed validation can cause automatic redirects. So you are probably getting redirected to a different URL here, with a status code that makes the client keep the same request method (likely 307 Temporary Redirect), but _that_ route of this second URL doesn’t actually exist for POST. – CBroe Jul 08 '20 at 08:35
  • @Monika I don't think that is a good idea – bhucho Jul 08 '20 at 08:35
  • Try using cURL (or other alternative) just to eliminate the possibility that this is a postman artefact – apokryfos Jul 08 '20 at 08:53

3 Answers3

1

Though validate also works fine but as mentioned by @CBRoe failed validation can cause automatic redirects. You can try the other method.

public function addTrip(Request $request) {
        $validator = Validator::make($request->all(), [
            'date' => ['required','regex:/^(2020-08-([0-2][0-9]|30))$/'],
            'user_id'=> ['required','regex:/^[0-9]*$/'],
            'train_id'=> ['required','regex:/^(1|2)$/'],
            'train_route_id'=> ['required', 'regex:/^(1|2)$/'],
            'stop_from'=> ['required','regex:/^[1-8]$/'],
            'stop_to'=> ['required','regex:/^[1-8]$/'],
        ]);
        if ($validator->fails()) {
            return Redirect::back()->withErrors($validator);
        }
        
        $data = request()->post();

        DB::table('trips')->insert([
            ['user_id' => $data['user_id'], 'train_id' => $data['train_id'], 'train_route_id' => $data['train_route_id'], 'stop_from' => $data['stop_from'], 'stop_to' => $data['stop_to'], 'date' => $data['date'], 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
        ]);
        

As a suggestion you can also use 'in' validation for train_id, train_route_id. See here

bhucho
  • 3,903
  • 3
  • 16
  • 34
0

Laravel redirects when validation fails. So you are redirecting to a route which is may be a GET route. As you are trying from Postman you can send a key value pair in your request header.

accept:application/json

Laravel will send a json response with validation errors like below instead of redirection.

{
    "message": "The given data was invalid.",
    "errors": {
        "date": [
            "The date does not match the format"
        ]
    }
}
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
-1

Check if you have registered another route above this route.

You may also want to try php artisan cache:clear or php artisan route:cache.

Spholt
  • 3,724
  • 1
  • 18
  • 29
Shukri Yusof
  • 40
  • 1
  • 6