Active Inactive Status Using Toggle Button/Slide Laravel 8


In this tutorial, I like to show you how to create active inactive functionality in laravel 8 applications. We can implement change status using ajax with a bootstrap toggle button in laravel. Here we will update the active inactive status in laravel.

Almost require to create an active inactive functionality in laravel. it might be required for user status, product status, category status, etc. we have always enabled or disabled, active and inactive, etc. you can do it this toggle status active inactive PHP example this time.

In this example we will create users listing page and give bootstrap toggle button using bootstrap-toggle js. so you can easily enable and disabled it. using the bootstrap-toggle js change event we will write jquery ajax code and get or post a request to change the user statue field on the database.

Discord - How To Toggle Game Activi...
Discord - How To Toggle Game Activity Button

So, let's see follow a few steps and get status change functionality with an example, bellow also attach a screenshot of the layout.

Active Inactive Status Using Toggle Button/Slide Laravel 8
Step 1: Install Laravel 8

if you haven't laravel 8 application setup then we have to get a fresh laravel application. So run the below command and get a clean fresh laravel application.

composer create-project --prefer-dist laravel/laravel toggleLaravel
Step 2: Create Routes

In this, a step we need to create a route for user listing and another one to save data. so open your routes/web.php file and add the following route.

routes/web.php

Route::get('users', '[email protected]');
Route::get('changeStatus', '[email protected]');
Step 3: Create Controller

Now we should create new controller as UserController. This controller will manage layout and getting data request and return response, so put bellow content in controller file:

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::get();
        return view('users',compact('users'));
    }
  
    /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function changeStatus(Request $request)
    {
        $user = User::find($request->user_id);
        $user->status = $request->status;
        $user->save();
  
        return response()->json(['success'=>'Status change successfully.']);
    }
}
Step 4: Create View

let's create users.blade.php for layout and we will write design code here and put following code:

resources/views/users.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Active Inactive Status Using Toggle Button/Slide Laravel 8 - phpcodingstuff.com</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" ></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"  />
    <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
    <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
</head>
<body>
    <div class="container">
        <h1>Laravel Update User Status Using Toggle Button Example - ItSolutionStuff.com</h1>
        <table class="table table-bordered">
            <thead>
               <tr>
                  <th>Name</th>
                  <th>Email</th>
                  <th>Status</th>
               </tr> 
            </thead>
            <tbody>
               @foreach($users as $user)
                  <tr>
                     <td>{{ $user->name }}</td>
                     <td>{{ $user->email }}</td>
                     <td>
                        <input data-id="{{$user->id}}" class="toggle-class" type="checkbox" data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Active" data-off="InActive" {{ $user->status ? 'checked' : '' }}>
                     </td>
                  </tr>
               @endforeach
            </tbody>
        </table>
    </div>
</body>
<script>
  $(function() {
    $('.toggle-class').change(function() {
        var status = $(this).prop('checked') == true ? 1 : 0; 
        var user_id = $(this).data('id'); 
         
        $.ajax({
            type: "GET",
            dataType: "json",
            url: '/changeStatus',
            data: {'status': status, 'user_id': user_id},
            success: function(data){
              console.log(data.success)
            }
        });
    })
  })
</script>
</html>

Now we are ready to run our example so run below commands quick run:

php artisan serve


Now you can open the below URL on your browser:

http://localhost:8000/users

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close