How To Use Laravel 10 Blade Components Example

Reading Time: 5 minutes

Blade components in Laravel 10 offer a streamlined and reusable way to organize and manage UI elements across web applications. Leveraging Blade components allows developers to encapsulate HTML elements with associated logic into reusable components, enhancing code organization and readability.

In this tutorial, we’ll see the comprehensive process of using Blade components in Laravel 10. This functionality empowers developers to create modular and reusable UI components, reducing code redundancy and improving maintainability within Laravel applications.

Read More: How To Generate Test Users in Laravel 10 Tutorial

Let’s get started.

What are Laravel Blade Components?

Laravel Blade Components are a powerful feature introduced in Laravel 7 that allows you to create reusable and encapsulated UI components for your web application. They provide a clean and organized way to manage and reuse chunks of HTML and logic across multiple views.

Benefits of Blade Components:

  • Reusability: Components can be reused across different views, promoting a modular and DRY (Don’t Repeat Yourself) approach.
  • Encapsulation: Components encapsulate their markup and logic, making code more maintainable and easier to understand.
  • Dynamic Content: Components can handle dynamic data and actions, making them versatile for various use cases.
  • Consistency: Helps in maintaining a consistent UI across the application by using standardized components.

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


Assuming laravel already installed inside your system.

Create a Basic “Button” Blade Component

Components are typically stored in the resources/views/components directory.

To create a blade component,

php artisan make:component Button

It will create a blade view file button.blade.php inside resources/views/components folder and also creates a component class Button.php inside app/View/Components folder.

Open button.blade.php (layout which displays in user interface) file. It define the HTML structure and any required logic for your button component

<button {{ $attributes->merge(['class' => 'bg-blue-500 text-white']) }}>
    {{ $slot }}

In this example, we use the $attributes variable to combine any additional attributes passed to the component. When used in a view, the $slot variable represents the content inserted into the component.

Read More: How to Paginate Laravel 10 API Resources Tutorial

How To use Blade Component?

Open any Blade view (e.g., resources/views/welcome.blade.php) and include your component using the <x> directive.

    Click Me

The text “Click Me” will be rendered inside the button component.

How To Reuse Components with Parameters

According to use, You may need to supply parameters to your components for modification.

For example, let’s make it possible to change the color of our button component.

<x-button color="red">

To do so, navigate to the app/View/Components directory and declare a public property called ‘color’ in the Button.php class.

Open file and write this complete code into it,


namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Button extends Component
    public $color;

    * Create a new component instance.
    public function __construct($color = 'red')
        $this->color = $color;

    * Get the view / contents that represent the component.
    public function render(): View|Closure|string
        return view('components.button');

Read More: How To Use Laravel 10 Queue To Send Emails Tutorial

Now, you can use the $color property inside the button.blade.php view.

<button {{ $attributes->merge(['class' => 'bg-' . $color . ' text-white']) }}>
    {{ $slot }}

That’s it.

We hope this article helped you to learn about How To Use Laravel 10 Blade Components Example in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more