I have a very strange issue with deleteing records. In my vuejs I call axios.delete to delete my record, which in turn calls my laravel route.
The record is getting deleted fine but an error message is displaying "message": "The DELETE method is not supported for this route. Supported methods: GET, HEAD.",
axios.delete('/member/event/' + this.data.module.slug);
My laravel route is as follows
Route::resource('event', 'EventController');
I am using laravel 6 to
It's high chance that delete url you try to send and route url are not the same, please check network and route:list, if not then you probably define your spa route at top, I mean the route except every params
The issue was nothing to do with the routes it was that I was not returning a json response after the delete which was causing the issue
Try this way-
routes routes/api.php
Route::apiResource('event','EventController');
In Controller Method
public function destroy(Event $event)
{
$event->delete();
return new EventResource($event);
}
In Your Components
axios.delete('/api/event/'+this.data.module.slug)
.then(response => {
console.log(response)
this.$snotify.success("Data Successfully Delete",'Success')
})
.catch(err => {
console.log(err)
})
Related
i've registered a post route and set the "auth" middleware on it,everything is working properly but at the last step when i want to redirect to my registered route after authentication,an error occurred, it seems redirect() helper function uses GET method by default,but my route supports POST method.is there any way to use redirect() with POST method?!!
Route::post('match', 'HomeController#match')->name('match')->middleware('auth');
and inside my LoginController:
if ($is_match==='comes_from_match') {
return redirect()->route('match');
}else{
return redirect()->route('dashboard');
}
this leads to the following error:
"The GET method is not supported for this route. Supported methods: POST."
Try the following:
You can use any instead of post it works for both get and post.
Route::any('match', 'HomeController#match')->name('match')->middleware('auth');
As per latest Laravel documentation:
Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:
You can use match or any method:
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
Reference:
Laravel -> Routing -> Basic Routing
Im trying to build SPA and I need to make axios call to endpoint in my routes. How can make this api call work?
Here are my routes
Route::get('/{any}', function () {
return view('default');
})->where('any', '.*');
Route::get('events', 'EventsController#index')->prefix('api');
Any suggestion?
You must go to the folder routes->api and create your api routes, there is automatically the api prefix.
in your .vue
axios.get('/api/getUser')
.then(data => {
console.log(data)
});
in your folder routes->api
Route::get('/getUser', 'Api\UsersController#getUser');
To not put "/api" in front of all your axios calls in your '.vue' you can put
axios.defaults.baseURL = '/api';
in your app.js
We define a catch-all route to the SpaController which means that any web route will map to our SPA. If we didn’t do this, and the user made a request to /hello, Laravel would respond with a 404.
Route::get('/{any}', 'SpaController#index')->where('any', '.*');
Remember that this route will always be putted on the last part of your web routes after declaring your other routes
use this
Route::any('{all}', function () {
return view('default');
})
->where('all', '^(?!api).*$')
->where('all', '^(?!storage).*$');
Route::get('events', 'EventsController#index')->prefix('api');
as you will need api and storage route handel by laravel
I am trying to get a laravel-nuxt project running. I am stuck with creating route calls to my laravel backend using axios async call to serve up data to my nuxt frontend before loading the page.
I am constantly getting getting a 404 with my current laravel-nuxt setup even though I have the route defined in api.php.
I am using this as a template for the project and I have not changed anything in that template yet:
https://github.com/cretueusebiu/laravel-nuxt
So my frontend call is this here:
async asyncData ({ $axios }) {
if (process.server) {
return $axios.$get('/api/data')
.then((res) => {
this.data = res.data;
})
}
}
And my backend route is defined as follows in api.php:
Route::get('/data', 'HomeController#index');
It always gives me a 404, is there something missing that I should be aware of?
According to the Readme in the Github project you have mentioned, you have to add your routes manually to
client/router.js
Read this line under Notes and follow the structure well you'll be able to avoid this.
This project uses router-module, so you have to add the routes
manually in client/router.js.
hope this helps.
I have get some documents from Laravel Documentation.But i can't get details clearly from that. There are lot of routing methods and how to use that for my requirements? Commonly most people are using this, but what are the other routing methods?
Route::get()
Route::post()
How to pass the message or values through this Routing? Using Controller like this is a only way?
Route::get('/app', 'AppController#index');
Types of Routing in Laravel
There are some Routing methods in Laravel, There are
1. Basic GET Route
GET is the method which is used to retrieve a resource. In this example, we are simply getting the user route requirements then return the message to him.
Route::get('/home', function() { return 'This is Home'; });
2. Basic POST Route
To make a POST request, you can simply use the post(); method, that means when your are submitting a Form using action="myForm" method="POST", then you want to catch the POST response using this POST route.
Route::post('/myForm', function() {return 'Your Form has posted '; });
3. Registering A Route For Multiple Verbs
Here you can retrieve GET request and POST requests in one route. MATCH will get that request here,
Route::match(array('GET', 'POST'), '/home', function() { return 'GET & POST'; });
4. Any HTTP Verb
Registering A Route Responding To Any HTTP Verb. This will catch all the request from your URL according to the parameters.
Route::any('/home', function() { return 'Hello World'; });
Usage of Routing in Laravel
When your are Using the Route::, Here you can manage your controller functions and views as follows,
1. Simple Message Return
You can return a simple message which will display in the webpage when user request that URL.
Route::get('/home', function(){return 'You Are Requesting Home';});
2. Return a View
You can return a View which will display in the webpage when user request that URL
// show a static view for the home page (app/views/home.blade.php)
Route::get('/home', function()
{
return View::make('home');
});
3. Request a Controller Function
You can call a function from the Controller when user request that URL
// call a index function from HomeController (app/Http/Controllers)
Route::get('/home', 'HomeController#index');
4. Catch a value from URL
You can catch a value from requested URL then pass that value to a function from Controller. Example : If you call public/home/1452 then value 1452 will be cached and will pass to the controller
// call a show function from HomeController (app/Http/Controllers)
Route::get('/home/{id}', 'HomeController#show');
You can get help about routing from Laravel.
There are 4 methods ofform data sending that you must know --
Route::get for <form method="GET">
Route::post for <form method="POST">
Route::put for <form method="PUT"> -- This one is for updating your database, I recommend you to use laravelcollective/html, like this -- {!! Form::open(['method' => 'PUT']) !!}, but in your web browser you can find the method as POST only
Route::delete for <form method="DELETE"> -- This one is for deleteing a field in your database, I recommend you to use laravelcollective/html, like this -- {!! Form::open(['method' => 'DELETE']) !!}, but in your web browser you can find the method as POST only
There are many much you have to know about Laravel Routing, like CRUD, etc.
I have set up a resource controller using jefferyway's laravel4 generator as player.
So when i go to the url /players/show it shows me the show.blade.php. That's correct. But when I go to the /players/{whatever name field i can pass} it goes to the show.blade.php. No error thrown of httpnotfoundexception or anything.
These are the controller and routes file for the application.
http://paste.laravel.com/qwp
http://paste.laravel.com/qwq
That's the way it's supposed to work.
The show method on line 45 handles GET requests to /players/{anything}.
Jeffery Way has a really nice screencast series on Laravel 4, and he explains this in detail:
Resourceful Controllers: Part 1
Resourceful Controllers: Part 2
When you register a resource controller, it will create those routes for you :
GET /players players.index PlayerController#index
GET /players/create players.create PlayerController#create
POST /players players.store PlayerController#store
GET /players/{players} players.show PlayerController#show
GET /players/{players}/edit players.edit PlayerController#edit
PUT /players/{players} players.update PlayerController#update
PATCH /players/{players} PlayerController#update
DELETE /players/{players} players.destroy PlayerController#destroy
You can have this list with : php artisan routes
You can now see players.show will handle /players/*
Use Example:
Route::group(array('before' => 'auth'), function()
{
Route::get('/', function()
{
// Has Auth Filter
});
Route::get('user/profile', function()
{
// Has Auth Filter
});
});