Reputation:
I am trying to write an endpoint which exports the data from the users table i currently have into a CSV file which is downloaded upon clicking of a button. I have setup the controller, routes, export file, and added in the button within the view but everytime i go to click the button it just directs me to the admin/user/export
then gives me a 404 but shouldnt it just stay on the same URL then just add the file to my downloads file?
This is the package i am using for Laravel
https://github.com/maatwebsite/Laravel-Excel
web route
Route::get('users/export', 'Admin\UserController@export')->name('users.export');
UserExport
class UsersExport implements FromCollection
{
public function collection()
{
return User::all();
}
}
Controller Function
public function export()
{
return Excel::download(new UsersExport, 'users.csv');
}
Summarise problem:
Everytime i click the button on the view page it directs to admin/users/export
then gives me a 404 when i want it to just download the CSV file for the users.
Some help to see where i am wrong would be helpful!
Thanks.
Upvotes: 1
Views: 2730
Reputation: 8252
The problem is that two of your routes conflict with each other.
Given the following two routes:
Route::get('users/{user}', 'Admin\UserController@show')->name('users.show');
Route::get('users/export', 'Admin\UserController@export')->name('users.export');
Currently Laravel assumes that when you are trying to access users/export
you actually want to access users/{user}
with export
as the route parameter {user}
.
Making sure that users/export
is registered before users/{user}
should solve your issue:
Route::get('users/export', 'Admin\UserController@export')->name('users.export');
Route::get('users/{user}', 'Admin\UserController@show')->name('users.show');
Upvotes: 0