DEV Community

Cover image for Laravel 7 Blade Components
Ajay Yadav
Ajay Yadav

Posted on

2 2

Laravel 7 Blade Components

Blade is a templating engine in laravel that allows you to use plan php in your view.In laravel 7 developers were introduced with new class based blade syntax for creating components. If you are familiar with VueJs components you will find the idea is same but in PHP way.

By creating a blade component you are following DRY ( don't repeat yourself) principle . it means you can reuse it in your project.

So lets begin :
first create a component by this command :

 php artisan make:component Alert
Enter fullscreen mode Exit fullscreen mode

This command will generate two files
app\View\Components\Alert.php

this file handle variables and functions of the blade component.

resources\views\components\alert.blade.php

Now you can call this component in your project by "<x-alert>" , so you can see "x" is used to access component ,
now we want to pass a variable name "title" in the component

<x-alert title="This is title"> </x-alert>
Enter fullscreen mode Exit fullscreen mode

now open "app\View\Components\Alert.php" and add title variable in the class


<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Alert extends Component
{
    /**
     * The alert title.
     *
     * @var string
     */
    public $title;

    /**
     * Create the component instance.
     *
     * @param  string  $title
     * @return void
     */
    public function __construct($type)
    {
        $this->title= $title;
    }

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


Enter fullscreen mode Exit fullscreen mode

Now $title property is accessible in our "alert" blade component. you can define more variables here and can access it in the blade component like you can pass message or type of the alert.

more examples for the alert messages


here we have passed two variables, "type" is similar like title we passed in above example and second is message but in the message variable we are passing a PHP variable value.

now update your alert.blade.php with

<!-- /resources/views/components/alert.blade.php -->
<div class="alert alert-{{ $type }}">
    {{ $message }}
</div>
Enter fullscreen mode Exit fullscreen mode

Real life example:

<!-- /resources/views/components/alert.blade.php -->
<div {{ $attributes->merge(['class' => 'p-6 rounded-lg shadow-lg']) }}>
   <div class="text-xl text-orange-500">{{ $title }}</div>
    <div class="mt-4">
        {{ $slot }}
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

call it in your project

<x-panel title="Update Your Info!" class="max-w-2xl">
    <h1>I AM IN THE SLOT</h1>
</x-panel>
Enter fullscreen mode Exit fullscreen mode

Thank you,🤗

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay