How to Create Reusable Components in Laravel? (ok)

https://www.itsolutionstuff.com/post/how-to-create-reusable-components-in-laravelexample.html

ok

C:\xampp\htdocs\reset\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('my-components', function () {
  return view('my_components');
});

C:\xampp\htdocs\reset\resources\views\components\card.blade.php

<div class="card {{ $class }}">
  <h5 class="card-header">{{ $title }}</h5>
  <div class="card-body">
    <p class="card-text">{{ $slot }}</p>
  </div>
</div>

C:\xampp\htdocs\reset\resources\views\my_components.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to create reusable blade components in Laravel - ItSolutionStuff.com</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css" />
</head>
<body>
<div class="container">
    <h3>How to create reusable blade components in Laravel - ItSolutionStuff.com</h3>
    <!-- For Primary -->
    @component('components.card')    
        @slot('class')
            bg-primary
        @endslot
        @slot('title')
            This is from ItSolutionStuff.com(Primary)
        @endslot
        @slot('slot')
            My components with primary
        @endslot
    @endcomponent
    <br/>
    <!-- For Danger -->
    @component('components.card')    
        @slot('class')
            bg-danger
        @endslot
        @slot('title')
            This is from ItSolutionStuff.com(Danger)
        @endslot
        @slot('slot')
            My components with primary
        @endslot
    @endcomponent
    <br/>
    <!-- For Success -->
    @component('components.card')    
        @slot('class')
            bg-success
        @endslot
        @slot('title')
            This is from ItSolutionStuff.com(Success)
        @endslot
        @slot('slot')
            My components with primary
        @endslot
    @endcomponent
</div>
</body>
</html>

How to Create Reusable Components in Laravel?

You can easily create blade component in laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 using this example.

In this example, we will create one card component file and we will reuse that file in our other blade file. i used bootstrap card so, you just need to add $class, $title and $slot variable. so you have to just follow two step to done that example.

You can see preview of using components.

Create Component File

In this step, we will create new folder for components and create card blade file for our component as like bellow:

resources/views/components/card.blade.php

<div class="card {{ $class }}">    <h5 class="card-header">{{ $title }}</h5>    <div class="card-body">       <p class="card-text">{{ $slot }}</p>    </div></div>

Reuse Component

Now we will create one route with blade file. on that blade file we will reuse our created component on that file with different classes as like bellow:

Let's create route and blade file:

routes/web.php

Route::get('my-components', function () {    return view('my_components');});

resources/views/my_components.blade.php

Read Also: Laravel Gates and Policies Tutorial with Example

<!DOCTYPE html><html><head>    <title>How to create reusable blade components in Laravel - ItSolutionStuff.com</title>    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css" /></head><body>  <div class="container">    <h3>How to create reusable blade components in Laravel - ItSolutionStuff.com</h3>       <!-- For Primary -->    @component('components.card')               @slot('class')            bg-primary        @endslot          @slot('title')            This is from ItSolutionStuff.com(Primary)        @endslot          My components with primary    @endcomponent      <br/>      <!-- For Danger -->    @component('components.card')              @slot('class')            bg-danger        @endslot          @slot('title')            This is from ItSolutionStuff.com(Danger)        @endslot          My components with primary    @endcomponent      <br/>       <!-- For Success -->    @component('components.card')              @slot('class')            bg-success        @endslot          @slot('title')            This is from ItSolutionStuff.com(Success)        @endslot           My components with primary    @endcomponent  </div>  </body></html>

Now you can run and check it.

I hope it can help you.

Last updated