简体   繁体   中英

Laravel jQuery - Check every minute if certain row exists in database

Currently I'm working on a project which requires a little jQuery to get something done. The thing I need to get done is to check every minute if a certain row with a certain ID in the database exists.

I have a blade file where the user gets to when he or she finished a certain step. Within this blade file, There needs to be a jQuery script that checks every minute if a row with a certain webshop->id exists in the database table called "Droplets". If It does, A progress bar needs to be set a certain width. How can I achieve that?

My attempt so far looks like this:

HTML (Progress-install)

<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>

The jQuery script needs to check if there is a row in the table called "Droplet" with the webshop->id which I can call by doing {{ webshop->id }} in my blade file

jQuery attempt

$(document).ready(function() {

    var checkdb = function () {
        $.ajax({
           type: 'POST',
           url: '/droplet/get/' {{ $webshop->id }},
           data: '_token = <?php echo csrf_token() ?>',
           success:function(data) {
               $("#dropletInfo").html(data.info)
           }
        });

        var ele = document.getElementById('progress-install');
        ele.style.width = 30+'%';

    };
    setInterval(checkdb(),1000 * 60);
})

The route that the jQuery script calls is

Route::post('/droplet/get/{id}', 'DropletController@getAll')->name('getAllDroplets');

and looks like this:

public function getAll($id)
{
    $info = Droplet::where("webshop_id", "=", $id)->get();

    return response()->json(array($info));
}

How can I achieve the above?

Here your url is not build correctly:

url: '/droplet/get/' {{ $webshop->id }},  // here you are missing the contenation, so change it to:

url: '/droplet/get/' + {{ $webshop->id }},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM