Defer loading on a widget

Following livewire's docs I don't seem to be able to get this working, has anyone had success deferring loading on a widget?
class MyWidget extends BaseWidget
{
public bool $readyToLoad = false;

public function loadData()
{
$this->readyToLoad = true;
}

protected function getStats(): array
{
if (! $this->readyToLoad) {
return [
Stat::make('Loading', 'Loading...')
];
}

sleep (5);

return [
Stat::make('Loaded Widget', 'Done!'),
];
}
}
class MyWidget extends BaseWidget
{
public bool $readyToLoad = false;

public function loadData()
{
$this->readyToLoad = true;
}

protected function getStats(): array
{
if (! $this->readyToLoad) {
return [
Stat::make('Loading', 'Loading...')
];
}

sleep (5);

return [
Stat::make('Loaded Widget', 'Done!'),
];
}
}
Solution:
```php $isLazy = true; public function placeholder(): View...
Jump to solution
8 Replies
pepperoni dogfart
pepperoni dogfartOP7mo ago
The issue is more so when they are visible, some of the widgets take a while to calulate. Would prefer to be able to show a placeholder while that's happening
Solution
LeandroFerreira
LeandroFerreira7mo ago
$isLazy = true;

public function placeholder(): View
{
return view('loading');
}
$isLazy = true;

public function placeholder(): View
{
return view('loading');
}
<!-- resources/views/loading.blade.php -->
<div>loading..</div>
<!-- resources/views/loading.blade.php -->
<div>loading..</div>
pepperoni dogfart
pepperoni dogfartOP7mo ago
Could not check compatibility between App\Filament\Widgets\MyWidget::placeholder(): App\Filament\Widgets\View and Filament\Widgets\Widget::placeholder(): Illuminate\Contracts\View\View, because class App\Filament\Widgets\View is not available
Could not check compatibility between App\Filament\Widgets\MyWidget::placeholder(): App\Filament\Widgets\View and Filament\Widgets\Widget::placeholder(): Illuminate\Contracts\View\View, because class App\Filament\Widgets\View is not available
LeandroFerreira
LeandroFerreira7mo ago
use Illuminate\Contracts\View\View; ?
pepperoni dogfart
pepperoni dogfartOP7mo ago
Aye, sorry, yes. long day of converting a customers excel formulas into a filament page haha
LeandroFerreira
LeandroFerreira7mo ago
worked?
pepperoni dogfart
pepperoni dogfartOP7mo ago
it did thanks!
Want results from more Discord servers?
Add your server