Reputation: 31
I created filter to get data from database. When I use GET method it works but with POST method I get error:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
I spent hours looking for solution but seems like I am missing something here.
For test I tried to get results directly in routes but it's still same problem.
This works.
<?php
use App\test;
Route::get('/', function () {
$test = test::all();
return $test;
});
This doesn't work.
<?php
use App\test;
Route::post('/', function () {
$test = test::all();
return $test;
});
Upvotes: 1
Views: 14897
Reputation: 505
Please note that if you are writing the route for post, when testing it make sure that your are posting the data to the page otherwise it will through an error.
Post routes cannot test simply by calling the url, the method should be post. If you check the route URI using postman it will work.
Upvotes: 0
Reputation: 19
It's look like you may forgot about {{ csrf_field() }}. Add this to your form.
If you are posting data without the csrf, in laravel 5.5 you will see this error.
Upvotes: 1
Reputation: 62368
Route::get()
and Route::post()
are defining the route handlers for the specified http methods.
If you only define this route:
Route::post('/', function () {
$test = test::all();
return $test;
});
Then you must make sure all calls to that url use the POST method. If you make a request to that url with the GET method, you'll get the MethodNotAllowedHttpException
exception, since you only defined the POST method handler.
Upvotes: 2
Reputation: 1165
This
use App\test;
Route::post('/', function () {
$test = test::all();
return $test;
});
Should always be an instance of Request
so you can't access it directly in the browser like GET
you have to post some form data to it. So it's better you rename it to something, maybe like:
use App\test;
Route::post('/test', function (Request $request) {
$test = test::all();
return $test;
});
This $request
hold your form data
Upvotes: 2