更新和过滤laravel数据表

sbtkgmzw  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(138)

我想返回一个数据表,其中包含数组中的一个集合,我可以从浏览器的网络检查器中看到响应和预览选项卡,所有数据都显示在那里,但由于某种原因,表为空,如果我尝试在foreach循环中返回数据表,则只返回一个结果,但显示在表中
我有两个问题,
1.如何获取必须在datatable上显示的数据。
1.填写日期输入并单击筛选按钮后,如何按日期筛选数据
这是将订单带到页面的函数

public function anyData(){
        
        $orders = Order::simplePaginate(10);
        foreach ($orders as $order){
      
            $id = $order->purchaser_id;
            $user = \App\Models\User::where('id', '=', $id)->first();
            $user_category = \App\Models\UserCategory::where('user_id', $id)->value('category_id');
            $category = \App\Models\Category::where('id', $user_category)->value('name');
            $referrer = \App\Models\User::where('id', '=', $user->referred_by)->first();
            $order_item = \App\Models\OrderItem::where('order_id', $order->id)->first();
            $quantity = $order_item->qantity;
            $product = \App\Models\Product::where('id', $order_item->product_id)->first();
            $price = $product->price;
            $order_total = $price * $quantity;
            
            if ($referrer) {
                $referred_distributors = \App\Models\User::where('referred_by', $referrer->id)
                    ->where('enrolled_date', '<', $order->order_date)
                    ->count();
            
                if ($referred_distributors < 5) {
                    $percentage = 5;
                } elseif ($referred_distributors >= 5 && $referred_distributors <= 10) {
                    $percentage = 10;
                } elseif ($referred_distributors >= 11 && $referred_distributors <= 20) {
                    $percentage = 15;
                } elseif ($referred_distributors >= 21 && $referred_distributors <= 30) {
                    $percentage = 20;
                } elseif ($referred_distributors >= 31) {
                    $percentage = 30;
                }
              
            }else{
                $referred_distributors = '0';
                $percentage = '0';
            }

            $datas[] = collect([
                ['order' => $order],
                ['user' => $user],
                ['referrer' => $referrer],
                ['referred_distributors' => $referred_distributors],
                ['percentage' => $percentage],
                ['commision' => ($percentage * $order_total)],
               
            ]);
            
        }

      
        return app('datatables')->collection($datas)
                ->addIndexColumn()
                ->addColumn('action', function($row){
                    $actionBtn = '<a href="#invoice" class="invoice more" data-toggle="modal"
                    data-target="#invoice" data-id="$order->id">View Item</a>';
                    return $actionBtn;
                })
                ->rawColumns(['action'])
                ->make(true);
      
     
        

       // return Datatables::of(User::query())->make(true);
    }

此表显示当前工作正常的数据

<table class="table table-bordered" id="users-table">
                            <div class="col-md-3 mb-5">
                                <h2>Date from</h2>
                                <input type="date" class="form-control" id="min" name="min"
                                    data-date-split-input="true">
                            </div>
                            <div class="col-md-3 mb-5">
                                <h2>Date to</h2>
                                <input type="date" class="form-control" id="max" name="max"
                                    data-date-split-input="true">
                            </div>

                            <thead>
                                <tr>
                                    <th scope="col">Invoice</th>
                                    <th scope="col">Purchaser</th>
                                    <th scope="col">Distributor</th>
                                    <th scope="col">Referred Distributors</th>
                                    <th scope="col">Order Date</th>
                                    <th scope="col">Percentage</th>
                                    <th scope="col">Commission</th>
                                    <th scope="col"></th>
                                </tr>
                            </thead>
                        </table>

                        @push('scripts')
                            <script>
                                $(function() {
                                    $('#users-table').DataTable({
                                        processing: true,
                                        serverSide: true,
                                        ajax: "{{ route('datatables.data') }}",

                                        columns: [{
                                                data: 'order.id',
                                                name: 'order'
                                            },
                                            {
                                                data: 'user.first_name',
                                                name: 'user.first_name'
                                            },
                                            {
                                                data: 'referrer.first_name',
                                                name: 'referrer.first_name'
                                            },
                                            {
                                                data: 'referred_distributors',
                                                name: 'referred_distributors'
                                            },
                                            {
                                                data: 'order.order_date',
                                                name: 'order.order_date'
                                            },

                                            {
                                                data: 'percentage',
                                                name: 'percentage'
                                            },
                                            {
                                                data: 'commision',
                                                name: 'commision'
                                            },

                                        ]
                                    });
                                });
                            </script>
                        @endpush

如果我最终获得了要在表中显示的数据,那么现在我的第二个问题是,当单击筛选按钮时,如何筛选日期输入。
谢谢

vc6uscn9

vc6uscn91#

我建议使用dataTables。
https://datatables.net/
或者更好的是,使用laravel数据表:https://datatables.yajrabox.com/
我还在开发一个简化范围过滤的库:https://laraveltesting.itulbuild.com/documentation/slickFilters(仍在进行中)
关于如何处理列上的范围过滤,您需要了解的一切都在上面的slickFilter.js文档中概述(即使您不使用该库)。
----编辑----
我注意到您的javascript列定义看起来不正确。
这可能更接近你想要的

columns: [{
        data: 'invoice',
        name: 'order.id'
    },
    {
        data: 'purchaser',
        name: 'user.first_name'
    },
    {
        data: 'distributer',
        name: 'referrer.first_name'
    },
    {
        data: 'referred_distributors',
        name: 'referred_distributors'
    },
    {
        data: 'order_date',
        name: 'order.order_date'
    },

    {
        data: 'percentage',
        name: 'percentage'
    },
    {
        data: 'commision',
        name: 'commision'
    },
    {
        data: 'action',
        filterable: false,
        sortable: false
    }
]

相关问题