为啥我的 laravel 集合返回 500 错误,即使不是在我使用 dd() 检查结果时?

Posted

技术标签:

【中文标题】为啥我的 laravel 集合返回 500 错误,即使不是在我使用 dd() 检查结果时?【英文标题】:Why is my laravel collection returning 500 error even it's not when I checked the results using dd()?为什么我的 laravel 集合返回 500 错误,即使不是在我使用 dd() 检查结果时? 【发布时间】:2021-10-17 17:07:30 【问题描述】:

我这里有一个 Yajra 数据表,它显示基于另一个数据表的记录。意思是第一个表的记录,当单击时,会在另一个内部数据表(#tbl-received-items)中显示该记录的记录。目前,我遇到一个错误:

DataTables warning: table id=tbl-received-items - Ajax error. For more information about this error, please see http://datatables.net/tn/7

当我在 chrome 中看到网络选项卡时,出现 500 错误并显示:

表示结果有问题。请帮我找出我的 createReceivedItemsCollection() 函数中的错误。

我可以确认我的$collection 正在返回正确的集合。请看截图。

这是我的路线:

Route::get('receiving-history', [ReceivingHistoryController::class, 'index'])->name('receiving-history.index');
Route::get('receiving-history/list', [ReceivingHistoryController::class, 'getReceivingHistory']);
Route::get('receiving-history/voucher_id', [ReceivingHistoryController::class, 'show'])->name('receiving-history.show');
Route::get('received-items/list', [ReceivingHistoryController::class, 'getReceivedItems']);

这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\
    Product,
    ReceiveItems
;
use DataTables;

class ReceivingHistoryController extends Controller

    public function index() 
        return view('receiving-history.index');
    

    public function show(Request $request, ReceiveItems $receive_items) 
        if ($request->ajax()) 
            $data = $this->createReceivedItemsCollection($request);
            return Datatables::of($data)->make(true);
        
        return view('receiving-history.show', ['receive_items' =>  $receive_items]);
    

    public function getReceivingHistory(Request $request) 
        if ($request->ajax()) 
            $data = ReceiveItems::latest()->get();
            return Datatables::of($data)->make(true);
        
    

    public function getReceivedItems(Request $request) 
        if ($request->ajax()) 
            $data = $this->createReceivedItemsCollection($request);
            return Datatables::of($data)->toJson();
        
    

    public function createReceivedItemsCollection(Request $request) 
        $received_items = ReceiveItems::where('voucher_id', '=', $request->voucher_id)->get();
        foreach($received_items as $received_item) 
            $product_ids = $received_item->product_id;
            $list = []; 
            foreach($product_ids as $product_item_no => $product_id) 
                $products = Product::where('id', '=', $product_id); 
                $voucher_cost = $products->value('cost');
                $qty_addend = $received_item->qty_per_item;

                $list[] = array(
                    'product_item_no' => $product_item_no + 1,
                    'product_id' => $product_id,
                    'product_name' => $products->value('name'),
                    'size' => $products->value('size'),
                    'qty_addend' => $qty_addend[$product_item_no],
                    'voucher_cost' => $voucher_cost,
                    'ext_cost' => number_format($voucher_cost * $qty_addend[$product_item_no], 2)
                );
            
            $list = (object)$list;
            $collection = collect($list);
        
        return $collection;
    

我的数据表 JS 文件:

$(document).ready(function () 
    $.noConflict();
    var voucherID = document.cookie;
    // =datatable
    let table = $('#tbl-received-items').DataTable(
        processing: true,
        language:  
            processing: '<div class="loader-container"><div class="loader"></div></div>'
        ,
        ajax: "/received-items/list",
        serverSide: true,
        columns: [
            
                data: 'product_item_no',
                name: 'product_item_no',
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                orderable: true, 
                searchable: true, 
                render : function(data, type, row) 
                    return '<span class="text-sm font-medium text-gray-900 content">' + data + '</span>';
                ,
            ,
             
                data: 'product_name', 
                name: 'product_name', 
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                orderable: true, 
                searchable: true, 
                render : function(data, type, row) 
                    return '<a href="'+ getBaseUrl() + '/products/' + row.product_id + '"><div class="text-sm text-gray-900 content">' + data + '</div></a>';
                ,  
            ,
             
                data: 'size', 
                name: 'size',
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                render : function(data, type, row) 
                    return '<a href="'+ getBaseUrl() + '/products/' + row.product_id + '"><div class="text-sm font-medium text-gray-500 content">' + data + '</div></a>'; 
                ,
            ,
             
                data: 'qty_addend', 
                name: 'qty_addend',
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200 col-qty-addend',
                render : function(data, type, row) 
                    return '<span class="text-sm font-medium text-gray-500 content">' + data + '</span>'; 
                ,
            ,
             
                data: 'voucher_cost', 
                name: 'voucher_cost',
                orderable: true, 
                searchable: true,
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                render : function(data, type, row) 
                    return '<a href="'+ getBaseUrl() + '/products/' + row.product_id + '"><div class="text-sm font-medium text-gray-500 content">' + data + '</div></a>'; 
                ,
            ,
             
                data: 'ext_cost', 
                name: 'ext_cost',
                orderable: true, 
                searchable: true,
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                render : function(data, type, row) 
                    return '<span class="text-sm font-medium text-gray-500 content">' + data + '</span>'; 
                ,
            ,
        ],
        oLanguage:  sProcessing: "<div id='loader'></div>" ,
        footerCallback: function(row, data, start, end, display) 
            var api = this.api(),
                data;
                
            var intVal = function(i) 
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '') * 1 :
                    typeof i === 'number' ?
                    i : 0;
            ;

            total = api
                .column(5)
                .data()
                .reduce(function(a, b) 
                    return intVal(a) + intVal(b);
                , 0);

            $('span#total').text(new Intl.NumberFormat('en-PH', 
                    style: 'currency',
                    currency: 'PHP',
                ).format(total));
        
    );
);

function getBaseUrl() 
    return window.location.protocol + "//" + window.location.host;

然后是我的 html 表格:

<table id="tbl-received-items" class="min-w-full tbl-responsive">
    <thead class="bg-gray-50">
        <tr>
            <th
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Product Item #
            </th>
            <th
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Product Name
            </th>
            <th
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Size
            </th>
            <th
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Qty
            </th>
            <th
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Voucher Cost
            </th>
            <th
                class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                Ext. Cost
            </th>
        </tr>
    </thead>
    <tbody class="bg-white">
    </tbody>
</table>

非常感谢任何帮助。

【问题讨论】:

【参考方案1】:

我设法通过在第一个数据表上添加一个 onclick 事件来解决这个问题,该数据表将 localstorage 设置为凭证 ID。另外,我将它设置为 POST 以传递数据。

 $('#tbl-receiving-history').on('click', 'tr', function () 
        $voucher_id = $(this).attr('data-voucher-id');
        sessionStorage.clear(); //clear first before setting localstorage
        localStorage.setItem("data-voucher-id", $voucher_id);
     );

然后在第二张表上,我只取localstorage的值:

$(document).ready(function () 
    $.noConflict();
    $voucher_id = localStorage.getItem('data-voucher-id');
    // =datatable
    let table = $('#tbl-received-items').DataTable(
        processing: true,
        language:  
            processing: '<div class="loader-container"><div class="loader"></div></div>'
        ,
        ajax: '/receiving-history/' + $voucher_id +  '/list',
        serverSide: true,
        columns: [
            
                data: 'product_item_no',
                name: 'product_item_no',
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                orderable: true, 
                searchable: true, 
                render : function(data, type, row) 
                    return '<span class="text-sm font-medium text-gray-900 content">' + data + '</span>';
                ,
            ,
             
                data: 'product_name', 
                name: 'product_name', 
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                orderable: true, 
                searchable: true, 
                render : function(data, type, row) 
                    return '<a href="'+ getBaseUrl() + '/products/' + row.product_id + '"><div class="text-sm text-gray-900 content">' + data + '</div></a>';
                ,  
            ,
             
                data: 'size', 
                name: 'size',
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                render : function(data, type, row) 
                    return '<a href="'+ getBaseUrl() + '/products/' + row.product_id + '"><div class="text-sm font-medium text-gray-500 content">' + data + '</div></a>'; 
                ,
            ,
             
                data: 'qty_addend', 
                name: 'qty_addend',
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200 col-qty-addend',
                render : function(data, type, row) 
                    return '<span class="text-sm font-medium text-gray-500 content">' + data + '</span>'; 
                ,
            ,
             
                data: 'voucher_cost', 
                name: 'voucher_cost',
                orderable: true, 
                searchable: true,
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                render : function(data, type, row) 
                    return '<a href="'+ getBaseUrl() + '/products/' + row.product_id + '"><div class="text-sm font-medium text-gray-500 content">' + data + '</div></a>'; 
                ,
            ,
             
                data: 'ext_cost', 
                name: 'ext_cost',
                orderable: true, 
                searchable: true,
                className: 'px-6 py-4 whitespace-no-wrap border-b border-gray-200',
                render : function(data, type, row) 
                    return '<span class="text-sm font-medium text-gray-500 content">' + data + '</span>'; 
                ,
            ,
        ],
        oLanguage:  sProcessing: "<div id='loader'></div>" ,
        createdRow: function (row, data, index) 
            if(Math.sign(data.qty_addend) == -1) $(row).addClass('bg-red-200');
        ,
        footerCallback: function(row, data, start, end, display) 
            var api = this.api(),
                data;
                
            var intVal = function(i) 
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '') * 1 :
                    typeof i === 'number' ?
                    i : 0;
            ;

            total = api
                .column(5)
                .data()
                .reduce(function(a, b) 
                    return intVal(a) + intVal(b);
                , 0);

            $('span#total').text(new Intl.NumberFormat('en-PH', 
                    style: 'currency',
                    currency: 'PHP',
                ).format(total));
        
    );
);

function getBaseUrl() 
    return window.location.protocol + "//" + window.location.host;

【讨论】:

以上是关于为啥我的 laravel 集合返回 500 错误,即使不是在我使用 dd() 检查结果时?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的路线返回 500 错误?

为啥我在作曲家安装后在 Laravel 中收到 500 服务器错误 [关闭]

为啥我的 Mongoose findAll 方法返回 500 错误 ..?

为啥我的路线返回 404 错误 Laravel?

Laravel发布路由返回错误500

Laravel 5 API 路由向前端返回 500 错误