首页 > 解决方案 > 未定义的属性:stdClass::$id 用于 laravel API

问题描述

我在 laravel 上使用 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),
        ];
    }
}

我刚刚删除了其他数组返回值以简化集合。并在 API 上作为 json 返回之前添加hashed_idpadded_id修改结果

我收到了这个错误

未定义的属性:stdClass::$id

是否可以从查询生成器中创建集合?如何?

标签: phplaravellaravel-5.5

解决方案


在查询中选择:

->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”。

您应该添加:

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

推荐阅读