In this video we are going to learn about the Blade Components.
Components provides benefits to sections and layouts.
There are two approaches to creating components.
First is class based components and 2nd is anonymous components.
To create a class based component, we have to use the make:component Artisan command.
So lets create a class component.
Switch to the command prompt and write the command.

php artisan make:component HeaderComponent

This command creates two files.
One file is created inside the app\\view\\components directory You can see here the header.php file.
This is component class file.
Which has render method, by default.
This component's render method just return the header view.
And second file which is created inside the resources/views/components/ folder.
You can see here header.blade.php file.
This is component view template file.
Now add some text to the header template file.
So I am just going to type here.


<h1>This is a header</h1>


Ok now save the file.
To display a component, We have to use a blade component tag Inside Blade templates.
Blade component tags start with the string x-.
followed by the name of the component class.
for display this component.
Go to the view file.
and from here just open welcome.blade.php file.
Inside the welcome.blade.php file just type here.


<x-header/>


Now save the file and lets check.
Switch to the browser and refresh the page.
You can see here header component is showing here Alright, Now pass some Data To the Components.
For that goto the welcome.blade.php and here just type.


<x-header name=\"Surfside Media\"/>


Alright now go to the header component class.
And inside the constructor just pass a parameter.
Which is $name and create a public class variable.


public $name;

public function __consruct($name)
{
$this->name = $name;
}


Now go to the header.blade.php.
and here just print the $name variable value.
For that just type here.

<h3>Hello {{$name}} </h3>

Save the file, now lets check it.
So switch to the browser and refresh the page.
You can see here \"hi surfside media\".
Alright, You can also pass the data from the controller.
So lets see, how can we pass data from the controller.
First off all lets create a controller.
So go to the command prompt.
and here just run the command.

php artisan make:controller HomeController

.
Now goto the HomeController and inside the homecontroller just create a method.
And inside the index method just create an array variable.

public function index()
{
$fruits = array('mango','orange','apple','pineapple','banana');
return view('welcome',['fruits'=>$fruits]);
}

Now goto the welcome blade.
and here just type.


<x-header name=\"Surfside Media\" :fruits=\"$fruits\" />


Save the file and go to the Header.php component class file.
And inside the constructor add one more parameter, $fruits.
and also create a public class variable.

public $fruits;

public function __consruct($fruits)
{
$this->fruits = $fruits;
}


Now print this fruits array to the header component view.
So goto the header.blade.php and just write the following code.

@foreach($fruits as $fruit)
<p>{{$fruit}}</p>
@endforeach.

Now save the file and finally modify the welcome route.
So open web.php file and here.
Just add.

Route::get('/', [HomeController::class, 'index']);

Save all.
Lets check.
Switch to the browser and refresh the page.
You can see here the fruits name.
So in this way can create and use blade component in laravel 8.