Laravel 7.x Update User Status Using Toggle Button Example

In this tutorial i will show you how to create user active and inactive status using bootstarp toogle button.i would like to show you how to create functionality to active and inactive status in laravel 7 application.It is pretty simple and very easy. 

we can implement change status using jquery ajax with bootstrap toggle button in laravel 7. here we will update user status active inactive with boolean data type with 0 and 1. you can do it this toggle stuff using jquery ajax.

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

So, let's see follow few step and get status change functionality with example, bellow also attach screen shot of layout.

Update-user-Status-using-Toggles-in-Laravel

 

Step 1: Install Laravel 6

Run bellow command to get a fresh laravel 6 app.

composer create-project --prefer-dist laravel/laravel ToogleChek

 

Step 2: Create Routes

 

Read also : Laravel 7 REST API with Passport Tutorial with Ecommerce Project

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

routes/web.php

Route::get('users', '[email protected]');
Route::get('changeStatus', '[email protected]');

 

Read also : Laravel 6 REST API with Passport Tutorial with Ecommerce Project

 

Step 3: Setup Controller

Now create userController and paste this following code to create active inactive user list using toggle button.

app/Http/Controllers/UserController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
   
    public function index()
    {
        $users = User::get();
        return view('users',compact('users'));
    }
  

    public function changeUserStatus(Request $request)
    {
        $user = User::find($request->user_id);
        $user->status = $request->status;
        $user->save();
  
        return response()->json(['success'=>'User status change successfully.']);
    }
}

 

Step 4: Create View

In Last step, create a file in this following directory resources/views/users.blade.php and paste this following code:

resources/views/users.blade.php

 

Now we are ready to run our app update user status using toggle button example.

 

Read also : Laravel Auth: Login With Email Or Username In One Field

Hope it can help you.