How to Create & Use Components in Laravel 8

Share this Article
Reading Time: 7 minutes
7,469 Views

Components are the re-usable section or layout which we can use inside laravel application. In laravel there are two methods to writing a components: class based components and anonymous components.

Learn More –

Inside this article we will see about complete concept of Components in Laravel 8 by the help of following points –

  • Create a Component
  • Files associated with a laravel component
  • Using Component in laravel
  • About Slots in Laravel
  • Passing Data to components
  • About Inline Component in Laravel
x
How to Create & Use Components in Laravel 8

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Create a Component

To create a class based component in laravel we use make:component artisan command. Back to terminal and type this command to create it.

$ php artisan make:component Message

When we run this, it creates few files into setup. Let’s see what are those files.

Files associated with a laravel component

make:component command creates a view template file as well as a Component class file. View layout file we will find inside /resources/views/components/message.blade.php. Along with this view file we also have a component class file. Class file we can find inside /app/View/Components/Message.php

Layout file of component provides the user interface means how component is presenting to application. Class file of component manages the component functionality. In class file we should see two methods as __construct() & render(). We will understand how we use these methods in a bit inside this ongoing article.


Using Component in laravel

In the last steps we have successfully created a component layout and it’s functional file. Next, we need to use it in application.

To use a component, we use a “blade component tag” within any template file or layout. Blade component tags start with the string x- followed by the kebab case name of the component class.

# Syntax

<x-{ComponentName}/>

In the above case we have component class name as Message. So it is pretty simple.

Example

/resources/views/about-us.blade.php Calling component inside this view file.

<x-message/>

When we have some cases like $ php artisan make:component DisplayMessage. To use this type of component we use.

<x-display-message/>

About Slots in Laravel

Slot is used to get the contents what we have given inside blade component tag. This handles the additional information related to slots.

Assume that we are calling component like this. Inside this we have some contents.

<x-message>
  
    <strong>Great!</strong> Student has been created.
  
</x-message>

To get the placed content from blade component tag, we use {{ $slot }} predefined variable in laravel. Back to component layout file –

<!-- /resources/views/components/message.blade.php -->

<div class="alert alert-danger">
  
    {{ $slot }}

</div>

{{ $slot }} variable going to print all the contents what we passed inside component tag.


Passing Data to components

We can pass two types of values. Static value and dynamic value. Let’s see how to pass data.

<x-message type="warning" :message="$message"/>
  • “type” is key which contains the value as warning. It has a static value.
  • “:message” is a key which contains dynamic value by means of variable.

Firstly we need to set the data attributes values to variable and then we need to send to layout file. To set these values, back to component class file. Open /app/View/Components/Message.php

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Message extends Component
{
    /**
     * The message type.
     *
     * @var string
     */
    public $type;

    /**
     * message.
     *
     * @var string
     */
    public $message;

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

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

Displaying data attributes to layout file

<div class="alert alert-{{ $type }}">
  
    {{ $message }}

</div>
  • {{ $type }} Blade syntax to print the value of $type sending from it’s class file.
  • {{ $message }} Blade syntax to print the value of $message.

About Inline Component in Laravel

Inline components are those components which basically generates only a single file i.e component class file. Inside the same class file we will have a render() method and piece of inline HTML code written inside it.

In regular components we have 2 files but in case of inline we have a single file.

Generate a Inline Component

To generate a inline component, we use make:component artisan command but this time we will a extra flag which is for inline.

$ php artisan make:component Message --inline

This command will generate something like this into the component file.

/**
 * Get the view / contents that represent the component.
 *
 * @return \Illuminate\View\View|\Closure|string
 */
public function render()
{
    return <<<'blade'
        <div class="alert alert-danger">
            {{ $slot }}
        </div>
    blade;
}

Inside above code we can see we have HTML markup inside render().

We hope this article helped you to learn about Components in Laravel 8 Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here