Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025

      I may have found the ultimate monitor for conferencing and productivity, but it has a few weaknesses

      June 2, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      May report 2025

      June 2, 2025
      Recent

      May report 2025

      June 2, 2025

      Write more reliable JavaScript with optional chaining

      June 2, 2025

      Deploying a Scalable Next.js App on Vercel – A Step-by-Step Guide

      June 2, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025
      Recent

      The Alters: Release date, mechanics, and everything else you need to know

      June 2, 2025

      I’ve fallen hard for Starsand Island, a promising anime-style life sim bringing Ghibli vibes to Xbox and PC later this year

      June 2, 2025

      This new official Xbox 4TB storage card costs almost as much as the Xbox SeriesXitself

      June 2, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Early View Data Preparation with Laravel View Creators

    Early View Data Preparation with Laravel View Creators

    January 3, 2025

    Early View Data Preparation with Laravel View Creators

    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 IlluminateSupportFacadesView;
    // Registering a View Creator
    View::creator('dashboard', DashboardCreator::class);
    

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

    <?php
    
    namespace AppViewCreators;
    
    use AppServicesMenuService;
    use IlluminateViewView;
    use IlluminateSupportFacadesAuth;
    
    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.


    The post Early View Data Preparation with Laravel View Creators appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleUsing AI to Manage Translations in Laravel
    Next Article Microsoft confirms Microsoft Rewards accounts suspended by mistake

    Related Posts

    Security

    ⚡ Weekly Recap: APT Intrusions, AI Malware, Zero-Click Exploits, Browser Hijacks and More

    June 2, 2025
    Security

    Qualcomm fixes three Adreno GPU zero-days exploited in attacks

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Elden Ring Nightreign Night Aspect: How to beat Heolstor the Nightlord, the final boss

    News & Updates

    SwDir.dll is Missing or Not Found: 5 Ways to Download it

    Operating Systems

    Hacktivist Groups Target NATO Summit Amid Rising Tensions

    Development

    JavaScript Essentials

    Development

    Highlights

    Thin Fonts Are a Usability Nightmare

    April 10, 2025

    Thin fonts are like that trendy minimalist couch that looks amazing in photos but is…

    The CyberPower UPS Vulnerability Threatening Critical Systems Across Sectors

    May 8, 2024

    nutgram/nutgram

    February 13, 2025

    Updated Debian 12: 12.7 released

    January 9, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.