Unable to add blade component using Appserviceprovider in laravel

Viewed 822

This is my Appserviceprovider code.

 public function boot()
    {
        Blade::component('components.badge', 'badge');
        //also tried app.components.badge  
     
    }

And this is my component badge.blade.php inside component folder in views folder

<div class="badge badge-{{ $type ?? 'success' }}">
    {{ $slot }}
</div>

And this is where m trying to display the component

 @badge(['type' => 'primary'])
        New!
 @endbadge

This is what m getting as an output on my page @badge(['type' => 'primary']) New! @endbadge

It is working when m using this

@component('components.badge', ['type' => 'primary'])
        New!
@endcomponent

enter image description here

2 Answers

In AppServiceProvider::boot(), change:

Blade::component('components.file','tag')

to

Blade::aliasComponent('components.file','tag')

Run php artisan view:clear to clear view cache.

The Blade::component method has to be renamed to Blade::aliasComponent in Laravel Framework 7.0

Using Blade::aliasComponent to give an alias to an anonymous component, the alias is not registered and an exception is thrown. Only In your App service provider, aliase that component as 'badge': /* AppServiceProvider.php */

From

Blade::component('components.badge', 'badge');

To

Blade::aliasComponent('components.badge', 'badge')
Related