Laravel Bootstrap Toggle switch Update DB field using Ajax – Technology

Laravel Bootstrap Toggle switch Update DB field using Ajax – Technology

In this post we will give you information about Laravel Bootstrap Toggle switch Update DB field using Ajax – Technology. Hear we will give you detail about Laravel Bootstrap Toggle switch Update DB field using Ajax – TechnologyAnd how to use it also give you demo for it if it is necessary.

Today, We want to share with you Laravel Bootstrap Toggle switch Update DB field using Ajax.In this post we will show you Bootstrap Toggle switch Update Database field using Ajax, hear for Create Stylish Toggles Checkboxes & Use in Form with Laravel 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 Laravel 5.7 with an exampleLaravel 5.7 Toggle switch Update DB field using Ajax.

Laravel Bootstrap Toggle switch Update DB field using Ajax

There are the Following The simple About Laravel Bootstrap Toggle switch Update DB field using Ajax Full Information With Example and source code.

As I will cover this Post with live Working example to develop Bootstrap Toggle to Update Database in Laravel 5.7, so the some major files and Directory structures for this example is following below Laravel Toggle Switch Inside Bootstrap Ajax Example.

Step 1: Include CDN and HTML Code

laravel-toggle-switch-button

<script defer src="https://use.fontawesome.com/releases/v5.0.4/js/all.js"></script>

<h2>Manage data using switch/toggle button on html with Laravel</h2>
<button  onclick="save">
  @if ($saved == true)
    <i id="save-icon" ></i><span id="save">Saved!</span>
  @else
    <i id="save-icon" ></i><span id="save">Save</span>
  @endif
</button>

Step 2: JavaScript Function

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

function save() {
    var saveEl = document.getElementById("save-icon");
    var iconType = saveEl.getAttribute("data-prefix");
}

Step 3: Ajax Http Call in jQuery

if ( iconType === 'fas' ) {
  $.ajax({
    url: "/unsave-product",
    cache: false,
    data: {
        slug: productSlug,
        userId: {{ $userId }}
    }
  })
  .done(function (response) {
      document.getElementById("save-icon").classList.add("far");
      document.getElementById("save").innerHTML="Save";
  })
  .fail(function () {
      console.log('failure');
  });
} else {
    $.ajax({
        url: "/save-product",
        cache: false,
        data: {
            slug: productSlug,
            userId: {{ $userId }}
        }
    })
        .done(function (response) {
            document.getElementById("save-icon").classList.add("fas");
            document.getElementById("save").innerHTML = "Saved!";
        })
        .fail(function () {
            console.log('failure');
        });
}

Step 4: Create A Laravel Controller

And Last to update query in Database to Save and unsave Product Details.

Another must read:  Laravel 6 Bootstrap Toggle Switch With Ajax Update To Mysql

public function unsave(Request $request) {
    $product = product::where('slug', $request->input('slug'))->first();
    $savedProduct = savedProducts::where([
        ['user_id', $request->input('userId')],
        ['product_id', $product->id]
    ])->first();

    $savedProduct->delete();
}

public function save(Request $request) {
    $product = product::where('slug', $request->input('slug'))->first();

    $savedProduct = new savedProducts();
    $savedProduct->user_id = $request->input('userId');
    $savedProduct->product_id = $product->id;
    $savedProduct->save();
}
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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

I hope you get an idea about Laravel Bootstrap Toggle switch Update DB field using Ajax.
I would like to have feedback on my onlinecode 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.

Hope this code and post will helped you for implement Laravel Bootstrap Toggle switch Update DB field using Ajax – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

1  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US