F
Filament15mo ago
Finn

Use a custom widget to run a command from the dashboard

Hi all, I'm looking to have the ability to run a command to generate a report for data on the dashboard, however there doesn't seem to be much documentation about how to use custom widgets unless I'm missing something? The desired functionality is for a user to click on a widget (or a button in the widget) on the dashboard which will run a script (probably a laravel console command) to generate a report and then download the report to the user's machine. Does anyone know how I might go about doing this? Specifically I'm asking for help RE: the widget / dashboard side of things. Thanks
Solution:
You should be able to run commands from the livewire side of the widget. Just put a button in it which triggers a function who in place runs the artisan command. Put this in the button: ```php wire:click="runCommand" //runCommand is a placeholder for your own function ofcourse...
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Jump to solution
7 Replies
Solution
biebthesecond
biebthesecond15mo ago
You should be able to run commands from the livewire side of the widget. Just put a button in it which triggers a function who in place runs the artisan command. Put this in the button:
wire:click="runCommand" //runCommand is a placeholder for your own function ofcourse
wire:click="runCommand" //runCommand is a placeholder for your own function ofcourse
https://laravel.com/docs/10.x/artisan
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Finn
FinnOP15mo ago
so are custom widgets essentially just livewire components? If I wanted to attach a custom URL to one would I put that in the view or the class?
biebthesecond
biebthesecond15mo ago
Oh and what do you mean with custom Url for a widget?
Finn
FinnOP15mo ago
Here's what I've got going on atm, my report generating functionality is happening in a controller method which is called by the route that the user is redirected to. StatusReport.php
<?php

namespace App\Filament\Widgets;

use Filament\Widgets\Widget;

class StatusReport extends Widget
{
protected static string $view = 'filament.widgets.status-report';

public function download()
{
return response()->redirectToRoute('download-status-report');
}
}
<?php

namespace App\Filament\Widgets;

use Filament\Widgets\Widget;

class StatusReport extends Widget
{
protected static string $view = 'filament.widgets.status-report';

public function download()
{
return response()->redirectToRoute('download-status-report');
}
}
status-report.blade.php
<x-filament-widgets::widget>
<x-filament::section>
<div>
<h1>Reports</h1>
<button type="button" wire:click="download">Download Status Report</button>
</div>
</x-filament::section>
</x-filament-widgets::widget>
<x-filament-widgets::widget>
<x-filament::section>
<div>
<h1>Reports</h1>
<button type="button" wire:click="download">Download Status Report</button>
</div>
</x-filament::section>
</x-filament-widgets::widget>
The only thing I'd like to do now is add some CSS to the widget, as currently everything looks the same. Would I have to register a custom stylesheet as a filament asset in the AppServiceProvider?
biebthesecond
biebthesecond15mo ago
Uhhh If I remember correctly you need to register a custom filament theme, but the last time I did that it was in filament V2. So I'm not sure, but it's somewhere in the docs for sure Ofcourse use tailwind for that :D
Want results from more Discord servers?
Add your server