Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql

Today, We want to share with you Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql.In this post we will show you Laravel Update User Status Using Toggle Button Example, hear for Bootstrap Toggle Switch Update Database Field Using Ajax we will give you demo and example for implement.In this post, we will learn about Bootstrap Toggle Switch With Ajax Update To Mysql In PHP Laravel 6 with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql

There are the Following The simple About Create Stylish Toggles Checkboxes & Use in Form with Laravel 6 Ajax Full Information With Example and source code.

As I will cover this Post with live Working example to develop Bootstrap Toggle switch Update DB field using Ajax Laravel 6, so the bootstrap toggle switch with ajax update to mysql in Laravel is used for this example is following below.

Phase 1: Install Laravel 6

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

Phase 2: Create Routes

routes/web.php

Route::get('members', '[email protected]');
Route::get('updateStatus', '[email protected]');

Phase 3: Create a Laravel 6 Controller

app/Http/Controllers/MemberController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Member;
  
class MemberController extends Controller
{
    /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $members = Member::get();
        return view('members',compact('members'));
    }
  
    /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function updateStatus(Request $request)
    {
        $member = Member::find($request->member_id);
        $member->status = $request->status;
        $member->save();
  
        return response()->json(['success'=>'Status change successfully.']);
    }
}

Phase 4: Create View

resources/views/members.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 6 Update User Status Using Toggle Button Example - pakainfo.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 6 Update User Status Using Toggle Button Example - pakainfo.com</h1>
        <table class="table table-bordered">
            <thead>
               <tr>
                  <th>Name</th>
                  <th>Email</th>
                  <th>Status</th>
               </tr> 
            </thead>
            <tbody>
               @foreach($members as $member)
                  <tr>
                     <td>{{ $member->name }}</td>
                     <td>{{ $member->email }}</td>
                     <td>
                        <input data-id="{{$member->id}}" class="toggle-class" type="checkbox" data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Active" data-off="InActive" {{ $member->status ? 'checked' : '' }}>
                     </td>
                  </tr>
               @endforeach
            </tbody>
        </table>
    </div>
</body>
<script>
  $(function() {
    $('.toggle-class').change(function() {
        var status = $(this).prop('checked') == true ? 1 : 0; 
        var member_id = $(this).data('id'); 
         
        $.ajax({
            type: "GET",
            dataType: "json",
            url: '/updateStatus',
            data: {'status': status, 'member_id': member_id},
            success: function(data){
              console.log(data.success)
            }
        });
    })
  })
</script>
</html>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about store value from on/off button in mysql database using Laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel Clear cache Tutorial With Example
  2.   Read Connection String from Web.Config file in ASP.Net using C# and VB.Net
  3.   WooCommerce Get Product Dimensions
  4.   Bootstrap Toggle switch with ajax update to mysql in Laravel
  5.   Find Factorial program in GO Example
  6.   Remove Last Character from String in PHP
  7.   Laravel Toggle Switch with Ajax Update Database
  8.   Laravel resource route multiple parameters Example
  9.   ASP.NET Ajax Control Tutorial with Examples
  10.   Laravel Ajax Bootstrap Toggle switch Update MySQL

Leave a Reply

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