Showing posts with label Laravel.Livewire. Show all posts
Showing posts with label Laravel.Livewire. Show all posts

Laravel Livewire User Active and Inactive Status Example

Laravel Livewire User Active and Inactive Status Example

Laravel Livewire User Active and Inactive Status Example

In this post, we'll walk through an example of using Laravel Livewire to toggle a user's status between active and inactive.

Setting Up the Livewire Component

First, generate a Livewire component:

php artisan make:livewire UserStatusToggle

Component Code

Here's the code for the UserStatusToggle component:


<?php

namespace App\\Http\\Livewire;

use App\\Models\\User;
use Livewire\\Component;

class UserStatusToggle extends Component
{
    public $user;

    public function toggleStatus()
    {
        $this->user->status = $this->user->status === 'active' ? 'inactive' : 'active';
        $this->user->save();
    }

    public function render()
    {
        return view('livewire.user-status-toggle');
    }
}
  

Blade View

Next, create the livewire.user-status-toggle Blade view:


<div>
    <button wire:click="toggleStatus">
        {{ $user->status === 'active' ? 'Set Inactive' : 'Set Active' }}
    </button>
</div>
  

Including Livewire in Your View

Make sure to include Livewire in your layout file:


<head>
    <!-- Other head content -->
    <livewire:styles />
</head>
<body>
    <!-- Your body content -->
    <livewire:scripts />
</body>
  

With this setup, you can easily toggle user statuses using Livewire!