Laravel Tutorials

Early View Data Preparation with Laravel View Creators

Published
Early View Data Preparation with Laravel View Creators image

Laravel's View Creators allow you to prepare data immediately after view instantiation, earlier than View Composers, making them perfect for setting up essential view data or optimizing performance.

use Illuminate\Support\Facades\View;
// Registering a View Creator
View::creator('dashboard', DashboardCreator::class);

Let's explore a practical example of managing a dynamic application menu:

<?php
	
namespace App\View\Creators;

use App\Services\MenuService;
use Illuminate\View\View;
use Illuminate\Support\Facades\Auth;

class ApplicationMenuCreator
{
    protected $menuService;
	
    public function __construct(MenuService $menuService)
    {
        $this->menuService = $menuService;
    }
	
    public function create(View $view)
    {
        $user = Auth::user();
        
        $view->with([
            'mainMenu' => $this->menuService->getMainMenu($user),
            'quickActions' => $this->menuService->getQuickActions($user),
            'recentItems' => $this->menuService->getRecentItems($user),
            'notifications' => $this->menuService->getPendingNotifications($user)
        ]);
    }
}

// In your AppServiceProvider
public function boot()
{
    View::creator('layouts.app', ApplicationMenuCreator::class);
}

// Usage in layouts/app.blade.php
<div class="sidebar">
    <nav>
        @foreach($mainMenu as $menuItem)
            <a href="{{ $menuItem['url'] }}" class="{{ $menuItem['active'] ? 'active' : '' }}">
                {{ $menuItem['label'] }}
            </a>
        @endforeach
    </nav>
    
    @if(count($quickActions))
        <div class="quick-actions">
            @foreach($quickActions as $action)
                <button onclick="handleAction('{{ $action['id'] }}')">
                    {{ $action['label'] }}
                </button>
            @endforeach
        </div>
    @endif
</div>

View Creators provide early data preparation for your views, ensuring critical data is available as soon as the view is instantiated.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article