Showing posts with label laravel change user status. Show all posts
Showing posts with label laravel change user status. Show all posts

Laravel User Active and Inactive Status Example

Laravel User Active and Inactive Status Example

Hi Men,

In this Blog, I can explain a way to create functionality to crate active and inactive user in laravel. I provide you with a complete instance of energetic and inactive user status in laravel. we can enforce exchange fame using ajax with bootstrap toggle button in laravel. we will replace user status lively inactive with boolean information type with zero and 1.

I almost require to create repute exchange capability in out laravel . it is probably require for user in laravel, product repute, category fame, and many others. we've continually yes or no, enable or disabled, lively and inactive and many others. you can do it this toggle stuff using jquery ajax.

We are able to create a customers listing web page and provide bootstrap toggle button the use of bootstrap-toggle js. so you can effortlessly allow and disabled it. using bootstrap-toggle js exchange occasion we can write jquery ajax code and fire get or submit request to trade person statue discipline on database.

Right Here following the example of active and inactive status in laravel

Step 1 : Install Laravel Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Route

In this is step we need to create route for active and inactive status layout file in laravel

following path:/routes/web.php
Route::get('users', 'UserStatusController@index');
Route::get('userChangeStatus', 'UserStatusController@userChangeStatus');
Step 3: Create Controller

Here this step now we should create new controller as UserStatusController,So run bellow command for generate new controller

php artisan make:controller UserStatusController

After this step,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:

following path:/app/Http/Controllers/UserStatusController.php
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserStatusController 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 userChangeStatus(Request $request)
    {
    	\Log::info($request->all());
        $user = User::find($request->user_id);
        $user->status = $request->status;
        $user->save();
  
        return response()->json(['success'=>'Status change successfully.']);
    }
}
Step 4: Create View

In Last step,we will write design code here and put following code

following path:/resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>How to Active and Inactive Status in Laravel? </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 class="bg-info">
    <div class="container">
      <div class="panel panel-default" style="margin-top: 40px">
        <div class="panel-heading">
          <h2>How to Active and Inactive Status in Laravel?  </h2>
        </div>
        <div class="panel-body">
          <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>
      </div>
    </div>
</body>
<script>
  $(function() {
    $('.toggle-class').change(function() {
        var status = $(this).prop('checked') == true ? 1 : 0; 
        var user_id = $(this).data('id'); 
         console.log(status);
        $.ajax({
            type: "GET",
            dataType: "json",
            url: '/userChangeStatus',
            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 bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/users

It will help you...