How to install Livewire to Laravel

How to install Livewire to Laravel 9

Posted by Ervin Adams on November 8, 2022

How to install Livewire to Laravel

How to install Livewire to Laravel 9

Posted by Ervin Adams on November 8, 2022

Livewire is a framework that allows you to build reactive, dynamic applications by using just the Laravel Blade template.

Let us install Livewire to Laravel:

Step 1. Run command below:

composer require livewire/livewire

Step 2. Create main blade file

Go to /resources/view and create app.blade.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Wecode101 Laravel Livewire</title>
        @livewireStyles
    </head>
    <body>
        @livewireScripts
    </body>
</html>

Step 3. Create component by running command below: 

php artisan make:livewire home

Step 4. Check that the companent has the render method:

 Look into /app/Http/Livewire/Home.php, the file should look like below:

<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Home extends Component
{
    public function render()
    {
        return view('livewire.home');
    }
}

Step 5. Add content to view file

A livewire view file was also created when you ran the command to create the component.

Go to /resources/views/livewire/home.blade.php and add code snippet below:

<div>
    <h1>Welcome Home</h1>
</div>

Step 6. Final, add component to app.blade.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Wecode101 Laravel Livewire</title>
        @livewireStyles
    </head>
    <body>
        @livewire('home')

        @livewireScripts
    </body>
</html>