未定义的属性:stdclass :: $ id for laravel api



我使用api

的查询构建器在laravel上这样做
$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  IlluminateHttpRequest
     * @return array
     */
    public function toArray($request)
    {
        return [
            "id"  => $this->id,
            "hashed_id" => hashids_encode($this->id),
            "padded_id" => id_padder($this->id),
        ];
    }
}

我刚刚删除了其他数组返回值以简化集合。并添加hashed_idpadded_id以修改结果,然后返回为JSON api

我有这个错误

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

是否可以从查询构建器中创建收集?如何?

在查询中选择:

->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",
    ...
)
$shipments = DB::table('shipment_batches')
            ->select(
                "shipments.id as id",
                "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);

最新更新