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

时间:2019-05-24 13:03:23

标签: php laravel laravel-5.5

我当时在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),
        ];
    }
}

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

我收到此错误

  

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

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

2 个答案:

答案 0 :(得分:0)

在查询中选择:

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

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

您应该添加:

{{1}}

答案 1 :(得分:-1)

$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);