How to Create Reusable Components in Laravel 8

Share this Article
Reading Time: 4 minutes
1,678 Views

Components are the re-usable section or layout which we can use inside laravel application. In laravel there are two methods to writing a components: class based components and anonymous components. Click here to learn complete details of components in laravel 8.

Learn More –

Inside this article we will see how to create reusable components in laravel 8 using components and slots. This article will be super easy to understand for the implementation.

Laravel provide components and slots for creating blade component sections in application.

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Create a Component File

Create a file with name message.blade.php inside /resources/views/components. You need to create a components folder as well inside it.

Open message.blade.php and write this complete code into it.

<div class="panel {{ $class }}">
    <div class="panel-heading">{{ $title }}</div>
    <div class="panel-body">{{ $slot }}</div>
</div>
  

Here, we have 3 placeholders as $class, $title and $slot.

When we will use this component then according to message type we will replace these values.


Create a Layout File

Create a my-template.blade.php blade layout file inside /resources/views folder.

Open my-template.blade.php and write this complete code into it.

<!DOCTYPE html>
<html>
<head>
    <title>How to Create Reusable Components in Laravel 8</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" />
</head>
<body>
  
<div class="container">
    <h3>How to Create Reusable Components in Laravel 8</h3>
   
    <div style="margin-top:20px;">
        <!-- For General info -->
        @component('components.message')    
    
            @slot('class')
                panel-primary
            @endslot

            @slot('title')
                This is from Online Web Tutor
            @endslot

            Custom components with primary class
        @endcomponent

        <br/>

        <!-- For Error -->
        @component('components.message')    

            @slot('class')
                panel-danger
            @endslot

            @slot('title')
                This is from Online Web Tutor
            @endslot

            Custom components with danger class
        @endcomponent

    <br/>

    <!-- For Success -->
    @component('components.message')    

        @slot('class')
            panel-success
        @endslot

        @slot('title')
            This is from Online Web Tutor
        @endslot

        Custom components with success class
    @endcomponent
    </div>
  
</div>
  
</body>
</html>

Code Explanation

  • @component(‘components.message’) … @endcomponent is used to use the component. It uses message.blade.php from components folder.
  • @slot(‘class’) … @endslot It used to place dynamic value for class ($class) placeholder. Same will be for $title placeholder.
  • Custom message – Custom components with success class will be rendered using $slot placeholder.

Add Route

Open web.php file from /routes folder. Add given route into it.

//...

Route::get('my-message', function () {

    return view('my-template');

});

//...

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL – http://127.0.0.1:8000/my-message

We hope this article helped you to learn about How to create reusable components in laravel 8 in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here