laravel-views
laravel-views copied to clipboard
Export to Excel
Is it possible to use the query result to make actions like export to PDF or Excel? How to manage the filtered result for doing some extra action?
Hi @samankassou, actually it is, there is query property on every view that you can access to, it is mostly for internal purposes, but as all the actions get the view instance as a param, they have access to this property.
public function handle($model, View $view)
{
dd($view->query);
}
The downside is that it only has the paginated results.
Yes @Gustavinho I saw it but this $view is accessible in an action, it means that we can perform only actions related to a row.
I know that we can register it in the bulkActions method but we have another problem, the user needs to select some rows before firing this action and more the data is paginated, this means that we can only have access to the 'visible' data like you mentioned it.
I have tried to solve the problem, but I know that it is not the 'good' way to this but it worked!
these are the steps:
- I make a button that emit an
exportToExcellivewire event - In my TableView, I added listener for this event
- In the method that handle this event, I use the
getRenderData()bult-in method to get theLengthAwarePaginatorarray - I use the
itemskey of thepaginatorto get the items and do my logic to export it to excel;
The problem is that I have only the first page of the paginated result I added an 'items per page' dropdown that user can set for the number of elements he wants to show before export them but It is not a good solution I think
Hi yes you're right, what I mentioned is for individual actions, and using a bulk action would make the user to select some rows before.
About what you're doing, I just would change the numbers 3 and 4, since you can have access to the $this->query property, it has the current query, the downside is that it is the paginated query as you saw.
I can do some internal refactors to have access to the query without pagination so we can use the whole data.
Either way, I think there is some room for a new type of feature, something like "table actions"