Rainieren
1 year ago

Laravel jQuery - Check if record exists in database

Posted 1 year ago by Rainieren

In my application, I build a page where the user sees a progress bar which displays the progress of an action that is happening in the background. One part of this progress bar is the following: I have to check in jQuery if a certain row with a certain id exists in the database. If it does exist, The progress bar has to skip to 30%. If it does not exists then It has to do nothing.

I have a database table called Droplets, In there-there is a field called Webshop_id. If a Droplet exists in this table which has the webshop_id of the one that the user just created, The progress bar goes to 30%.

A small summary to clarify

  • Check if a droplet with the webshop_id exists in the database using jQuery
  • If it does exist, Make the progress bar a width of 30%;
  • If it does NOT exists. Do nothing.

A simple if-statement in PHP of this issue would look something like this

if( Droplet::where("webshop_id", "=", $webshop->id)->exists())

    Make the progress bar 30% in width

else {

    Just do nothing

}

But this needs to be done in jQuery in order to make the progress bar a width of 30%. What is the best way to handle this issue?

The page where this needs to happen looks like this.

@extends('layouts.home') @section('content')

      <div class="row">
        <div class="col-md-12 text-right">
          <h3>37%</h3>
        </div>
        <div class="col-md-12 mb-3">
          <div class="progress mb-3">
            <div id="progress-install" class="progress-bar andcode-progress progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 10%"></div>
          </div>
        </div>
        <div class="col-md-12 text-right">
          <a href="" class="btn btn-orange disabled">See the result</a>
        </div>
      </div>
    </div>
  </div>
</div>
</div>
</div>

{{ $webshop->id }}

<script>
  $(document).ready(function() {
      var checkdb = function() {
   
      var ele = document.getElementById('progress-install');
      ele.style.width = 30 + '%';
   };
   setInterval(checkdb(), 1000 * 60);
 })
 </script>
 @endsection

I can call {{ $webshop->id }} anytime in this blade file. This ID has to match to the "Webshop_ID" in the droplets table, If there is a record in the db where the webshop_id matches the webshop_id in the droplets table. The progress bar has to skip to 30%. How can I achieve this?

Please sign in or create an account to participate in this conversation.