spatie/laravel-dashboard-twitter-tile
Laravel Dashboard tile that shows recent Twitter mentions. Built for Spatie’s laravel-dashboard, with configurable polling and display so you can surface who’s talking about your brand right on your internal dashboard.
Installation
composer require spatie/laravel-dashboard-twitter-tile
Publish the config file (if needed):
php artisan vendor:publish --provider="Spatie\DashboardTwitterTile\DashboardTwitterTileServiceProvider"
Configuration
Edit config/dashboard-twitter-tile.php to set:
@yourhandle)First Use Case
Add the tile to your dashboard in app/Providers/DashboardServiceProvider.php:
use Spatie\DashboardTwitterTile\DashboardTwitterTile;
public function boot()
{
Dashboard::create()
->row()
->tile(DashboardTwitterTile::class);
}
Visit /dashboard to see live Twitter mentions.
Dynamic Querying Override the default query terms via the tile constructor:
DashboardTwitterTile::make()
->query('@customhandle OR #hashtag')
->maxResults(5)
Caching & Performance Leverage Laravel’s cache for API rate limits:
// In config/dashboard-twitter-tile.php
'cache_key' => 'twitter_mentions_cache',
'cache_ttl' => 60, // seconds
Integration with Laravel Dashboard Combine with other tiles for a unified dashboard:
Dashboard::create()
->row()
->tile(DashboardTwitterTile::class)
->tile(DashboardStatsTile::class)
->row();
Customizing Display
Extend the tile’s view (resources/views/vendor/dashboard-twitter-tile/tile.blade.php) to modify:
Scheduled Refreshes Use Laravel’s scheduler to pre-fetch data:
// app/Console/Kernel.php
$schedule->command('dashboard:refresh-twitter-tile')->everyMinute();
(Requires adding a custom Artisan command.)
API Rate Limits
spatie/laravel-queueable-side-effects).Authentication Issues
BEARER_TOKEN in config is correct and has read permissions.curl -X GET "https://api.twitter.com/2/tweets/search/recent?query=@yourhandle" -H "Authorization: Bearer YOUR_TOKEN"
Deprecated API Endpoints
endpoint in config:
'endpoint' => 'https://api.twitter.com/2/tweets/search/recent',
Empty Results
@yourhandle vs. #hashtag).Error Handling Wrap the tile in a try-catch to show user-friendly messages:
try {
return DashboardTwitterTile::make()->render();
} catch (\Exception $e) {
return view('dashboard-twitter-tile::errors.generic', ['error' => $e->getMessage()]);
}
Local Development
Use tweetinvi for mocking Twitter responses in tests:
// tests/TestCase.php
use Tweetinvi\Mock\MockClient;
protected function getTwitterClient()
{
return new MockClient('mock_token');
}
Extending Functionality
getQuery() to dynamically filter tweets (e.g., by language):
public function getQuery()
{
return parent::getQuery() . ' lang:en';
}
public_metrics) by modifying the API request in getTweets().Environment-Specific Config Use Laravel’s config caching to avoid hardcoding secrets:
// .env
TWITTER_BEARER_TOKEN=your_token_here
Then reference it in config:
'bearer_token' => env('TWITTER_BEARER_TOKEN'),
Testing
spatie/laravel-test-factories to mock Twitter responses:
// tests/Feature/DashboardTwitterTileTest.php
public function test_tile_displays_mentions()
{
$this->mock(TwitterClient::class, function ($mock) {
$mock->shouldReceive('searchTweets')
->once()
->andReturn(['data' => [/* mock tweets */]]);
});
}
How can I help you explore Laravel packages today?