yajra datatable fractal从包含的属性中删除数据密钥

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yajra datatable fractal从包含的属性中删除数据密钥相关的知识,希望对你有一定的参考价值。

我想使用laravel-datatables-fractal使用Fractal转换服务器端响应。

首先,我为Product模型编写了一个变压器,如下所示:

class ProductTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'prices'
    ];

    public function transform(Product $product)
    {
        $transforms = [
            'product_id'     => $product->product_id,
            'code'           => $product->code,
            'title'          => $product->title,
            'description'    => $product->description,
        ];

        return $transforms;
    }

    public function includePrices(Product $product)
    {
        $prices = $product->prices;

        return $this->collection($prices, new PriceTransformer, FALSE);
    }
}

如您所见,有一个包含prices属性,该属性使用另一个名为PriceTransformer的变换器。

ProductController制作并返回所有产品的结果作为数据表格式的响应我有一个像这样的datatable方法:

public function datatable(Request $request)
    {
        return app('datatables')->of(Product::all())
            ->setTransformer(new ProductTransformer)
//            ->setSerializer(new CustomArraySerializer())
            ->make(TRUE);
    }

响应被返回,但我遇到的问题是:当我想返回prices包含的属性时,它返回为data属性,如下所示:

{
    "draw": 0,
    "recordsTotal": 8,
    "recordsFiltered": 8,
    "data": [
        {
            "product_id": 1,
            "title": "Product Title 1",
            "for_sale": true,
            "for_purchase": true,
            "changeable": false,
            "returnable": false,
            "sale_price": "40000.00",
            "purchase_price": "5000.00",
            "creator": 1,
            "created_at": "2017-12-11 12:21:49",
            "updated_at": "2017-12-14 11:55:52",
            "prices": {
                "data": [
                    {
                        "sale_price": "30000.00",
                        "created_at": "2017-12-11 12:21:49"
                    },
                    {
                        "sale_price": "40000.00",
                        "created_at": "2017-12-11 12:39:00"
                    },
                ]
            }
        },
        {
            "product_id": 11,
            "title": "Product Title 11",
            "for_sale": true,
            "for_purchase": true,
            "changeable": false,
            "returnable": false,
            "sale_price": "50000.00",
            "purchase_price": "40000.00",
            "creator": 1,
            "created_at": "2017-12-16 11:07:43",
            "updated_at": "2017-12-16 11:07:43",
            "prices": {
                "data": [
                    {
                        "sale_price": "50000.00",
                        "created_at": "2017-12-16 11:07:43"
                    }
                ]
            }
        }
    ],
    "input": {
        "include": "prices"
    }
}

我想从包含的属性中删除data,并且它们采用以下格式:

"prices": [
           {
            "sale_price": "50000.00",
            "created_at": "2017-12-16 11:07:43"
           }
          ]

即使我添加了一个序列化程序(正如您在上面的代码中看到的那样,并且已经评论过),但我将其用于所有其他响应并且运行良好:

class CustomArraySerializer extends ArraySerializer
{
    public function collection($resourceKey, array $data)
    {
        if ($resourceKey === FALSE) {
            return $data;
        }

        return ['success' => TRUE, 'result' => $data];
    }

    public function item($resourceKey, array $data)
    {
        if ($resourceKey === FALSE) {
            return $data;
        }

        return ['success' => TRUE, 'result' => $data];
    }
}

但是当使用它时我得到了这个错误:

{
    "draw": 0,
    "recordsTotal": 8,
    "recordsFiltered": 0,
    "data": [],
    "error": "Exception Message:

Undefined index: data"
}

什么是问题,如何解决?

答案

我正在使用Spatie / Transformer“spatie/laravel-fractal”,在添加上述包后尝试使用以下内容。

public function datatable(Request $request)
{
    return app('datatables')->of(Product::all())
    ->setTransformer(new ProductTransformer)
    ->serializeWith(new SpatieFractalisticArraySerializer())
    ->make(TRUE);
}

以上是关于yajra datatable fractal从包含的属性中删除数据密钥的主要内容,如果未能解决你的问题,请参考以下文章

yajra/laravel-datatables 搜索不适用于 laravel 5.4

yajra/laravel-datatables 搜索不适用于 laravel 5.7

如何将数据库索引与 Datatables 和 yajra/laravel-datatables 一起使用

Laravel Yajra Datatables 自定义按钮操作

laravel Yajra Datatable

Yajra DataTables:无法在表中加载记录