本文介绍了laravel API的未定义属性:stdClass :: $ id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel上使用API​​的查询生成器进行了此操作

I was doing this on laravel using query builder for API

$shipments = DB::table('shipment_batches')
            ->select(
                "shipments.id as shipment_id",
                "shipments.fulfillment_id",
                "shipments.shipped_address_id",
                "shipments.courrier_id",
                "shipments.status",
                "shipments.is_intro",
                "shipments.is_express",
                "shipments.shipment_batch_id",
                "shipments.intro_shipment_date",
                "shipments.intro_shipped_at",
                "shipments.tracking_number",
                "shipments.is_gift",
                "shipments.ship_unit",
                "shipments.shipment_notes",
                "shipments.is_one_time_product",
                "shipments.one_time_product_shipment_date",
                "shipments.one_time_product_shipped_at",
                "shipments.shipment_options",
                "shipments.is_migrated",
                "shipments.migration_has_error",
                "shipments.in_process_date",
                "shipments.shipment_date_at",
                "shipments.link",
                )
            ->join('shipments', 'shipments.shipment_batch_id', '=', 'shipment_batches.id')
            ->join('fulfillments', 'fulfillments.id', '=', 'shipments.fulfillment_id')
            ->join('subscriptions', 'subscriptions.id', '=', 'fulfillments.subscription_id')
            ->whereMonth('shipment_batches.shipment_date', '>', date('m'))
            ->whereYear('shipment_batches.shipment_date', '=', date('Y'))
            ->where('subscriptions.user_id', $id)
            ->orderBy('shipment_batches.id', 'ASC')
            ->get();

        return UpcomingShipmentsResource::collection($shipments);

我有这个资源集合

class UpcomingShipmentsResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            "id"  => $this->id,
            "hashed_id" => hashids_encode($this->id),
            "padded_id" => id_padder($this->id),
        ];
    }
}

我刚刚删除了其他数组返回值以简化集合.并添加了 hashed_id padded_id 来修改结果,然后在API上以json形式返回

I just removed other array return values to simplify the collection.and added hashed_id and padded_id to modify the result before returning as json on API

我收到此错误

是否可以从查询生成器中创建集合?怎么样?

Is it possible to create collection out of query builder?How?

推荐答案

在查询中选择:

->select(
                "shipments.id as shipment_id",
                "shipments.fulfillment_id",
                "shipments.shipped_address_id",
                "shipments.courrier_id",
                "shipments.status",
                "shipments.is_intro",
                "shipments.is_express",
                "shipments.shipment_batch_id",
                "shipments.intro_shipment_date",
                "shipments.intro_shipped_at",
                "shipments.tracking_number",
                "shipments.is_gift",
                "shipments.ship_unit",
                "shipments.shipment_notes",
                "shipments.is_one_time_product",
                "shipments.one_time_product_shipment_date",
                "shipments.one_time_product_shipped_at",
                "shipments.shipment_options",
                "shipments.is_migrated",
                "shipments.migration_has_error",
                "shipments.in_process_date",
                "shipments.shipment_date_at",
                "shipments.link",
                )

您没有选择"id",因此toArray()方法中将没有"$ this-> id".

You are not selecting "id", so you won't have "$this->id" in toArray() method.

您应该添加:

->select(
    "tablename.id as id",
    ...
)

这篇关于laravel API的未定义属性:stdClass :: $ id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:01