What is Components? 

Components are the reusable segment or layout which we can use inside the laravel application. It helps in time-saving and escapes the repetition of code.

Created a component by using a command.

php artisan make:component NameOfComponent

And this command provides a View in resources/views/components/nameofcomonent.blade.php and a PHP file in app/View/Components/NameOfComponent.php.

Once your component has been generated, it may be rendered using its name:<x-nameofcomonent/>


Create Header component

php artisan make:component Header

once you execute this command automatically, it will generate our component files 

┣ Header.php

┣ header.blade.php


    <p>Header component view</p>

Create views

Create views to render your component

┣ index.blade.php
┣ signin.blade.php


<h1>Header view</h1>


<h1>Signin View</h1>

Create routes


Route::view('/signin', 'signin');
Route::view('/', 'index');

go through the URL and see the output of the component is everywhere in the views

We can also pass variables in the component via attributes

<x-header : test="test value"/>

Pass the data

┣ Header.php



namespace App\View\Components;

use Illuminate\View\Component;

class Header extends Component
     * Create a new component instance.
     * @return void
    public $test;
    public function __construct($test)
        $this->test = $test;

     * Get the view / contents that represent the component.
     * @return \Illuminate\Contracts\View\View|string
    public function render()
        return view('components.header');

Are you facing problems in understanding this article?
You can watch the video. Watch it now