Laravel 9 How to Create Reusable Components & Use

Share this Article
Reading Time: 4 minutes
2,351 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.

Inside this article we will see laravel 9 how to create reusable components 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.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create a Reusable Component File

Create components folder inside /resources/views.

Create a file with name message.blade.php inside /components folder.

Open message.blade.php and write this 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 usage type we will replace these placeholder 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 code into it.

<!DOCTYPE html>
<html>
<head>
    <title>How to Create Reusable Components in Laravel 9</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 9</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>

Concept

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

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

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

        Custom components with success class  ----> slot
    @endcomponent

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 this 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 laravel 9 How to create reusable components in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5