简体   繁体   English

Laravel jQuery-检查数据库中是否存在行

[英]Laravel jQuery - Check if row exists in database

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. 该进度条的一部分如下:我必须在jQuery中检查数据库中是否存在具有特定ID的特定行。 If it does exist, The progress bar has to skip to 30%. 如果存在,进度条必须跳到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 . 我有一个名为Droplets的数据库表,其中有一个名为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%. 如果此表中存在一个Droplet ,其具有用户刚创建的一个webshop_id ,则进度条将达到30%。

A small summary to clarify 简要说明一下

  • Check if a droplet with the webshop_id exists in the database using jQuery 使用jQuery检查数据库中webshop_id存在带有webshop_id的小droplet

  • If it does exist, Make the progress bar a width of 30%; 如果存在,则使进度条的宽度为30%;

  • If it does NOT exists. 如果不存在。 Do nothing. 没做什么。

A simple if-statement in PHP of this issue would look something like this PHP在此问题中的简单if语句看起来像这样

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%. 但这需要在jQuery中完成,以使进度条的宽度为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="container" id="showEffect">
<div class="row justify-content-md-center">
<div class="col-md-10">
  <div class="card depth-5 p-4 mb-5">
    <div class="card-header p-4 bg-white">
      <div class="row">
        <div class="col-md-12">
          <h2 class="mb-4 text-green"><i class="fal fa-check-circle mr-3"></i> Payment succesfull!</h2>
        </div>
        <div class="col-md-8">
          <p class="text-muted m-0">Thank you! Your payment has been recieved! The order is confirmed. A reciept is being send to <b>{{ $order->user->email }}</b></p>
        </div>
      </div>
    </div>
    <div class="card-body">
      <div class="row">
        <div class="col-md-12">
          <h5>Summary</h5>
          <div class="card depth-2">
            <div class="card-body">
              <div class="row">
                <div class="col-md-6 text-right">
                  <ul class="list-unstyled">
                    <li class="text-muted">Payment ID:</li>
                    <li class="text-muted">Order date:</li>
                    <li class="text-muted">Payment method:</li>
                    <li class="text-muted">Total sum: </li>
                  </ul>
                </div>
                <div class="col-md-6 text-left">
                  <ul class="list-unstyled">
                    <li>{{ $payment->id }}</li>
                    <li>{{ $order->created_at->toFormattedDateString() }}</li>
                    <li>{{ $payment->method }}</li>
                    <li>€ {{ $order->price_sum }}</li>
                  </ul>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="card-footer mt-3 bg-white">
      <h4>Your Webshop</h4>
      <p class="text-muted">Your webshop is installing! We advise you not to click away if u want to prevent cancelling the installation</p>

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

<div class="andcode-clouds">

</div>
<script>
$(document).ready(function() {
var checkdb = function() {
  // Bestaat de droplet met de webshop id van de aangemaakte webshop in de database. Als deze wel bestaat, Spring naar 30%. Als hij niet bestaat. Dan niks
  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. 我可以随时在此刀片文件中调用{{ $webshop->id }} This ID has to match to the "Webshop_ID" in the droplets table 此ID必须与液滴表中的“ Webshop_ID”匹配

Make one controller function with your logic 用您的逻辑使一个控制器功能

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

       $id_exists = 1;

    else {

        $id_exists = 0;

    }

    return response()->json(['id_exists' => $id_exists]);
}

Then in your blade file 然后在刀片文件中

In script make ajax call to above controller function and get value of id_exists now you can decide whether to show progressbar or not upon this result 在脚本中,对上述控制器函数进行ajax调用并获取id_exists的值,现在您可以根据此结果决定是否显示进度条

You can use do this from database and controller validation in such way 您可以通过这种方式通过数据库和控制器验证来执行此操作

$table->string('abc')->unique();

This is your table column in migration file 这是迁移文件中的表格列

$this->validate($request,[
    'abc'=>'table_name:unique',
]);

This is your controller code in which abc is your request key and in $table->string('abc')->unique(); 这是您的控制器代码,其中abc是您的请求键,在$ table-> string('abc')-> unique();中; which is your column name of table and table_name is your table name you must replace with your own table name. 这是您表的列名,而table_name是您必须用自己的表名替换的表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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