Home » Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql

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.

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.

Read Also:  Laravel 6 Joins Example Tutorial

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 :

Read Also:  PHP prepend leading zero prefixes after number

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. Read Also:  cPanel custom PHP.INI File Configuration
  2. Read Also:  Laravel Ajax Bootstrap Toggle switch Update MySQL
  3. Read Also:  Create WordPress Custom Admin Page
  4. Read Also:  how to insert data from one database table to another database table in mysql?
  5. Read Also:  Bootstrap Toggle switch with ajax update to mysql in Laravel
  6. Read Also:  Getting Current Date Time in Vue.js Date Format
  7. Read Also:  Convert CSV Data to JSON using JavaScript with Example
  8. Read Also:  Laravel Bootstrap Toggle switch Update DB field using Ajax
  9. Read Also:  Toggle Switches Laravel, Ajax and jQuery example
  10. Read Also:  Check Armstrong Number in GO Program

Leave a Comment

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