Blade Component in Laravel 7

Laravel 7 Blade Component

Are you trying to get rid of writing same code again and again ? Imagine, you are working on multiple Laravel blade file which include a section which is common for all those blade files. What will you do ? Let me tell my own strategy, i will make a helper function which will render that blade file wherever this function is called on echo {{some_function()}} .

Laravel 7 has included a great feature to wipe this approach. This is a really cool feature and is called Laravel Blade Component. Laravel has now capability to make custom components and they can be called from anywhere.

Let’s start learning this feature.

Make a fresh Laravel 7 installation:

Make a fresh installtion by simply running this command from your terminal or cmd. 

Create Laravel 7 project

Now go for artisan help:

Simply run this command to see the usage and procedure to make a new component.

php artisan
php artisan help make:component

Make Component in Laravel 7

Now run your command to make new Blade Component:

Now finally tour blade component is made and ready to use:

Define your route and check component in App:


This is pretty similar to your controller. You can code here for fetching the data from database and pass it to component blade file. In short you can say it’s a mini controller.

Go to component blade file and write your html or blade codes:

Laravel component

Now the final step: Include this component in your regular blade file by just following a pretty new syntax

<x-navbar>
(Camel case name of your component)

Include laravel blade component

Here is the final output:

Conclusion:

Laravel blade component is really helpful in reducing the time and effort. We can now call this component from any blade file in our project. This is exactly what a Laravel developer will love to practice in his project. 

Er. Anupam Kumar
Latest posts by Er. Anupam Kumar (see all)
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments