Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Dashboard Laravel Package

spatie/laravel-dashboard

Build beautiful, Livewire-powered dashboards in Laravel. Provides base CSS, dashboard and tile view components, and a Tile model to persist fetched data so tiles can update themselves via polling.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require spatie/laravel-dashboard
    

    Publish the package assets and config:

    php artisan vendor:publish --provider="Spatie\Dashboard\DashboardServiceProvider"
    
  2. First Dashboard: Create a Livewire component (e.g., php artisan make:livewire MyDashboardTile) and register it as a tile:

    // app/Providers/AppServiceProvider.php
    use Spatie\Dashboard\Dashboard;
    
    public function boot()
    {
        Dashboard::tile('my-dashboard-tile', \App\Livewire\MyDashboardTile::class);
    }
    
  3. Display the Dashboard: Add the dashboard component to your layout (e.g., resources/views/layouts/app.blade.php):

    <x-dashboard>
        @dashboard
    </x-dashboard>
    
  4. First Tile: Create a simple Livewire component (MyDashboardTile) with a basic view:

    <!-- resources/views/livewire/my-dashboard-tile.blade.php -->
    <div>
        <h3>My Tile</h3>
        <p>Hello, Dashboard!</p>
    </div>
    

First Use Case: Real-Time Data Tile

Use the Tile model to cache data and enable polling:

// In your Livewire component
use Spatie\Dashboard\Tile;

public function mount()
{
    $this->tile = Tile::firstOrCreate(
        name: 'user-metrics',
        component: self::class
    );
}

public function render()
{
    $data = $this->fetchData(); // Your logic to fetch data
    $this->tile->data = $data;
    $this->tile->save();

    return view('livewire.my-dashboard-tile', ['data' => $data]);
}

public function fetchData()
{
    return User::count(); // Example: Fetch user count
}

Implementation Patterns

Workflows

  1. Tile Registration: Register tiles in AppServiceProvider or a dedicated service provider:

    Dashboard::tile('analytics', \App\Livewire\AnalyticsTile::class);
    Dashboard::tile('recent-activity', \App\Livewire\RecentActivityTile::class);
    
  2. Dynamic Tile Loading: Use conditional registration for admin-only tiles:

    if (auth()->user()->isAdmin()) {
        Dashboard::tile('admin-stats', \App\Livewire\AdminStatsTile::class);
    }
    
  3. Tile Groups: Organize tiles into logical groups (e.g., "Analytics", "Users") using Blade sections:

    <x-dashboard>
        <x-slot name="analytics">
            @dashboard('analytics')
        </x-slot>
        <x-slot name="users">
            @dashboard('users')
        </x-slot>
    </x-dashboard>
    

Integration Tips

  1. Livewire Polling: Enable automatic updates via Livewire's poll method:

    protected $listeners = ['refresh' => 'fetchData'];
    
    public function mount()
    {
        $this->poll(60000); // Refresh every 60 seconds
    }
    
  2. Data Persistence: Use the Tile model to store fetched data (e.g., for offline access or performance):

    $tile = Tile::firstOrNew(['name' => 'user-stats']);
    $tile->data = ['count' => User::count()];
    $tile->save();
    
  3. Custom Styling: Override the default CSS by publishing and extending the package styles:

    php artisan vendor:publish --tag=laravel-dashboard-public
    

    Then customize resources/css/dashboard.css.

  4. Authentication: Protect the dashboard with middleware:

    Route::middleware(['auth'])->group(function () {
        Route::get('/dashboard', function () {
            return view('dashboard');
        });
    });
    
  5. Multi-Tenant Support: Scope tiles to tenants using the tenant method:

    Dashboard::tile('tenant-stats', \App\Livewire\TenantStatsTile::class)
        ->tenant(Tenant::class);
    

Gotchas and Tips

Pitfalls

  1. Livewire Component Naming: Ensure Livewire component class names match the registered tile names (case-sensitive). Example:

    Dashboard::tile('user-metrics', \App\Livewire\UserMetrics::class); // Correct
    Dashboard::tile('user-metrics', \App\Livewire\Usermetrics::class); // Fails silently
    
  2. Polling Conflicts: Avoid registering multiple tiles with the same name, as it can cause Livewire to load the wrong component.

  3. Data Serialization: The Tile::data field uses JSON encoding. Ensure your stored data is serializable:

    // Bad: Non-serializable object
    $tile->data = new DateTime();
    
    // Good: Convert to string or array
    $tile->data = ['last_updated' => now()->toIso8601String()];
    
  4. Caching Headaches: Clear cached views and configurations after publishing assets:

    php artisan view:clear
    php artisan config:clear
    

Debugging

  1. Tile Not Rendering: Check if the tile is registered correctly:

    // Dump registered tiles
    dd(Dashboard::tiles());
    
  2. Livewire Updates Not Triggering: Verify the poll method is called in mount() and the component is properly registered.

  3. CSS Overrides Not Applying: Ensure your custom CSS is loaded after the package CSS in your layout:

    @vite(['resources/css/app.css', 'resources/css/dashboard.css'])
    

Tips

  1. Reusable Tile Logic: Extract common tile functionality into a base Livewire component:

    class BaseDashboardTile extends Component
    {
        use WithTileData; // Hypothetical trait for shared logic
    
        public function fetchData()
        {
            // Shared logic (e.g., caching, error handling)
        }
    }
    
  2. Tile Configuration: Pass configuration via the config method:

    Dashboard::tile('settings', \App\Livewire\SettingsTile::class)
        ->config(['poll_interval' => 30000]); // Custom poll interval
    
  3. Dark Mode Support: Use Tailwind’s dark mode classes or extend the package’s CSS for dark mode:

    @layer components.dashboard {
        .dark .dashboard-tile {
            background: #1a1a1a;
        }
    }
    
  4. Testing: Test tiles in isolation using Livewire’s testing helpers:

    $this->livewire(MyDashboardTile::class)
        ->assertSee('Hello, Dashboard!');
    
  5. Performance: Lazy-load tiles or use defer for non-critical components:

    @dashboard('analytics', defer: true)
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport