Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Laravel load anonymous components from packages

David Carr

Laravel Framework Packages

From Laravel 7 blade components are included. You may want to use these in packages.

For the components to be loaded from a package, the package service provider has to first load views from a folder and then use Blade::component to load the components.

Example:

In the package `src` folder create a folder called `components` 

This example is for a fictional package called `Elements`

The service provider loads the files from the components folder and then uses Blade::component to load each one. 

At the time of writing this, there is no way to automatically load components, they need to be loaded individually.

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;

class ElementsServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadViewsFrom(__DIR__ . '/components', 'elements');

        Blade::component('elements::form-button', 'form-button');
    }
}

These components can then use using by called <x- followed by the name:                                                                                                  

<x-form-button method="GET" action="/demo">
    Delete
</x-form-button>

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.