I am having problems resolving this issue. I want the user to be able to download a report when pressing the button. I keep on getting Action not defined, and if I change my route it will simply not load the app.
Any help would be appreciated.
/************View***************/
<button>Download PDF</button>
/*************Controller****************/
public function pdf($id){
$report=Report::findorFail($id);
$pdf = PDF::loadView('reports.show', compact('report'));
return $pdf->download('server_report.pdf');
}
You need to have your action defined in a route.
For example:
Route::get('pdf','ReportController#pdf');
Also, make sure that if ReportController has a resource route then the pdf route goes above it.
Related
I have a working Laravel project with loads of different routes.
I'm currently testing it and one of my tests was to check if a user were to use a delete or post route from the URL. I didn't know what the application would do honestly and it outputted the typical:
The GET method is not supported for this route. Supported methods: DELETE
which I have seen a million times. Is there a way to stop this error from coming up and instead output an error screen or simply redirect to a different view?
The error message:
The GET method is not supported for this route. Supported methods: DELETE.
should only appear when your Laravel site has APP_DEBUG set to true (in the .env file).
When APP_DEBUG is set to false as it should always be in on a live site, then the user will be shown a 404 error page instead.
You can easily test this by adding the following code to your routes file:
Route::delete('test', function() {
return 'You should never see this text when viewing url via a GET request';
});
May be u didn't noticed but ur form tag method attribute and route definition is different
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.
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');
I am loading the view in the package with
$this->loadViewsFrom(__DIR__.'../../resources/views', 'admin');
But when I request the page I get an error
InvalidArgumentException
View [test] not found.
Interesting twist, when I put the complete path to the view folder in the loadViewsFrom method the template loads.
I load the blade template with the following code
public function showTest()
{
return view('admin::test');
}
Right above the loadViewsFrom method, I call loadRoutesFrom method
$this->loadRoutesFrom(__DIR__.'../../routes/admin.php');
And this loads without any issue.
Any suggestions what could be the issue? The code of the package is very plain, load a blade template when a route is called.
The problem was solved by fixing the paths to view folder.
After verifying the exact location of my file by
dd(__DIR__);
I worked my way back to the correct path of the view folder, which was just one folder lower and not to.
I want shown message in laravel admin panel after action is executed or when i will redirect to listing or other page.
I have configured the laravel-admin package so inbuilt laravel module message showing in toaster so i want to need similar to this.
So can you please let me know if anyone know about this.
Thanks.
You can use this in case for redirecting:
admin_toastr(__('Outlet created successfully'));
return redirect(admin_url('/'));
Or in case of return after any action execution you use like below
public function sale(Request $request,Content $content) {
return $content->withSuccess('Success', 'Fund has been transfered successfully');
}
When I type an invalid url (non-existent controller), it displays the homepage rather than return a 404 Page Not Found page. Does anyone know the possible reason?
Thanks
Have checked if there is any plugins are registered in the bootstrap and if is it configured to catch the exceptions or set any ACL mechanisms.
Also check the error controller too. may be there is some forwarding methods implemented there.
Hi i've had the same problem and i now know how to fix it:
within the bootstrap have the following init
function _initModules()
{
$this->bootstrap('frontController') ;
$front = $this->getResource('frontController') ;
$front->addModuleDirectory(APPLICATION_PATH . "/modules");
$front->setParam("useDefaultControllerAlways", false);
}
Setting 'useDefault....' will use the default controller when a 404 occurs.
You should also make sure your error controller is setup correctly