Livewire V3 Navigate Feature

In this post I will share you Livewire V3 Navigate Feauture. that will help you to achieve SPA(single page application) Development using laravel livewire. lets start.. install livewire v3

first create a new laravel application

laravel new projectname

add below code in composer.js > require object

"livewire/livewire": "^3.0@beta"
composer update

Then go to database -> seeders -> DatabaseSeeder.php simple uncomment below line add 50 user

\App\Models\User::factory(50)->create();

then run your local server like xampp setup a data base

go to .env file add database credentials

DB_DATABASE=lvnavigate

Open terminal

php artisan migrate:fresh --seed

Create a controller

php artisan make:controller UserController

Go to controller create 3 function

public function index()
{
 return view('welcome');
}
    public function users()
    {
        $users = User::all();
        return view('users', compact('users');
    }
    public function contact()
    {
        return view('contact');
    }

Import User Model On top of the controller

use App\Models\User;

Go to routes -> web.php

Route::get('/', [UserController::class, 'index']);
Route::get('/users', [UserController::class, 'users']);
Route::get('/contact', [UserController::class, 'contact']);

Import controller from top in web.php

use App\Http\Controllers\UserController;

Create 3 page in resources -> View

contact.blade.php
users.blade.php
welcome.blade.php

Then create navbar component

php artisan make:component navbar --view

Go to resources -> views ->components – > navbar.blade.php

in this I am using bootstrap 5 you can use tailwind or custom css for design your blade file like below

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
      <a class="navbar-brand" href="{{ url('/') }}" wire:navigate>Navbar</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="{{ url('/') }}" wire:navigate>Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="{{ url('/contact') }}" wire:navigate>Contact</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="{{ url('/users') }}" wire:navigate>Users</a>
          </li>

        </ul>

      </div>
    </div>
</nav>

In anchor tag you add

Then design all pages. wire:navigate it will help to achieve spa behavior

welcome.blade.php file look like this

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Livewire Navigation</title>
</head>

<body>
    <x-navbar/>
    <div class="container mt-1">

        <h1>Home page</h1>
    </div>

    <!-- Optional <table class="table">JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous">
    </script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->
</body>

</html>

users.blade.php like below

<!doctype html>
<html lang="en">
  <head>
   
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">


    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Livewire Navigation</title>
  </head>
  <body>
    <x-navbar/>
    <div class="container mt-1">
        <livewire:userlist />
    </div>


    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>


  </body>
</html>

In contact.blade.php like below

<!doctype html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">


    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Livewire Navigation</title>
</head>

<body>
    <x-navbar/>
    <div class="container mt-1">
        <form>
            <div class="mb-3">
                <label for="exampleInputEmail1" class="form-label">Email address</label>
                <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
                <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
            </div>
            <div class="mb-3">
                <label for="exampleInputPassword1" class="form-label">Password</label>
                <input type="password" class="form-control" id="exampleInputPassword1">
            </div>
            <div class="mb-3 form-check">
                <input type="checkbox" class="form-check-input" id="exampleCheck1">
                <label class="form-check-label" for="exampleCheck1">Check me out</label>
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>


    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous">
    </script>

    
</body>

</html>

View Livewire navigate video

Thanks for visiting. if you have any issue please feel free to contact us.

you can view also:

How To check Installed PHP Version on Windows

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *