Installation:
composer require phpsa/filament-dadjokes
Publish the config file (if needed):
php artisan vendor:publish --provider="Phpsa\FilamentDadJokes\FilamentDadJokesServiceProvider"
Enable the Widget:
Add the DadJokesWidget to your Filament dashboard by including it in your Dashboard class:
use Phpsa\FilamentDadJokes\Widgets\DadJokesWidget;
public function widgets(): array
{
return [
DadJokesWidget::class,
];
}
First Use Case: Visit your Filament admin panel. The widget will automatically display a random dad joke on the dashboard.
Dashboard Integration:
widgets() method of your Dashboard class.sort property in the widget configuration:
DadJokesWidget::make()
->sort(1) // Lower numbers appear first
Service Selection:
'services' => [
'dad-jokes' => true,
'chuck-jokes' => false,
],
dad-jokes if no config is provided.Caching:
'cache' => 30, // Cache jokes for 30 seconds
5 seconds.Development Workflow:
php artisan config:clear to reset config changes during testing.Deployment Workflow:
config/filament-dadjokes.php before deployment.Custom Widget Styling: Extend the widget class to modify its appearance:
use Phpsa\FilamentDadJokes\Widgets\DadJokesWidget;
class CustomDadJokesWidget extends DadJokesWidget
{
protected static string $view = 'filament-dadjokes::custom-view';
}
API Rate Limiting: If using ChuckJokes, be mindful of API rate limits (typically 5 jokes per minute). Adjust cache duration to mitigate this.
Localization:
Override the joke display text in your language files (e.g., resources/lang/en/filament-dadjokes.php).
API Dependencies:
icndb.com for DadJokes, api.chucknorris.io for ChuckJokes`). Downtime or rate limits may cause failures.Caching Issues:
cache: 60) may result in stale jokes.5-10 seconds) or clear cache manually with php artisan cache:clear.Config Overrides:
.env or manual config.php artisan vendor:publish --provider="Phpsa\FilamentDadJokes\FilamentDadJokesServiceProvider"
Widget Visibility:
widgets() method of the Dashboard class.API Errors:
storage/logs/laravel.log) for API-related errors (e.g., cURL failures)..env to surface issues:
APP_DEBUG=true
Cached Jokes:
php artisan cache:clear
Widget Not Loading:
config/filament.php under widgets:
'widgets' => [
\Phpsa\FilamentDadJokes\Widgets\DadJokesWidget::class,
],
Extending Functionality:
getJoke() method in a custom widget class to fetch jokes from a local database or custom API:
public function getJoke(): string
{
return DB::table('jokes')->value('text');
}
Testing:
$this->mock(\Illuminate\Support\Facades\Http::class, function ($mock) {
$mock->shouldReceive('get')
->with('https://api.icndb.com/jokes/random')
->andReturn(Http::response(['value' => ['joke' => 'Test joke']]));
});
Performance:
Custom Joke Sources:
config/services.php to dynamically switch joke sources based on environment:
'services' => env('DAD_JOKES_SERVICE') === 'chuck' ? ['chuck-jokes' => true] : ['dad-jokes' => true],
How can I help you explore Laravel packages today?