当数据库中的项目数量低于特定数量(例如Laravel中的5)时,出现模式弹出窗口

时间:2019-09-13 13:39:49

标签: laravel bootstrap-modal laravel-5.8

我希望我的模态在库存商品的数量低于特定数字(如5)时在刀片中弹出我有模态,但我不知道如何调用/实例化它。请帮帮我。

https://laracasts.com/discuss/channels/laravel/models-views-controllers在这种讨论中没有解决方案,这就是为什么我恳求您的帮助。

这是index.blade.php中的模态:

<div class="modal fade" id="modal-danger">
            <div class="modal-dialog">
                <div class="modal-content bg-danger">
                    <div class="modal-header">
                        <h4 class="modal-title">Danger Modal</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <p>One fine body&hellip;</p>
                    </div>
                    <div class="modal-footer justify-content-between">
                        <button type="button" class="btn btn-outline-light" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-outline-light">Save changes</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>  

这是我的控制者:

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Sales Report</title>
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css">
            <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
            <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.bootstrap4.min.css">
            <link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.bootstrap4.min.css">
        </head>
        <body>
            <table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
                <caption><h3>Sales Report</h3></caption>
                <thead>
                    <tr>
                        <th>Sales ID</th>
                        <th>Stock ID</th>
                        <th>Product Name</th>
                        <th>Sale Quantity</th>
                        <th>Unit Selling Price</th>
                        <th>Total Sales Cost</th>
                        <th>Sold On</th>
                    </tr>
                </thead>
                <tbody>
                @foreach($sales as $key => $sale)
                    <tr>
                        <td>{{$sale->sales_id}}</td>
                        <td>{{$sale->stock_id}}</td>
                        <td>{{$sale->product_name}}</td>
                        <td>{{$sale->sale_quantity}}</td>
                        <td>{{$sale->unit_selling_price}}</td>
                        <td>{{$sale->total_sales_cost}}</td>
                        <td>{{$sale->updated_at}}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </body>
    </html>

以下是库存表的屏幕截图: screenshot of the stock table

1 个答案:

答案 0 :(得分:3)

我相信您有一个控制者可以返回您的视图。因此,只需计算库存项目并将其作为变量返回到您的视图,就像这样:

$items = Stock::count();

return view('your-view')->with([
  'itemsCount' => $items,
]);

然后,在您看来,执行以下操作:

<input type='hidden' value='{{ $itemsCount }}' id="items-count">

<script>
    $(document).ready(function(){
        if($("#items-count").val() < 5) {
            $("#your-modal-id").modal("show");
        }
    });
</script>

更新

计算特定库存;

$items = Stock::where('stock_name', $stockName)->first()->stock_quantity;
//If you want to count every single stock, just do it with a for or foreach loop
return view('your-view')->with([
  'itemsCount' => $items,
]);

希望这会有所帮助。