doudan1123 2019-05-24 13:03
浏览 189

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

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

And I have this resource collection

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),
        ];
    }
}

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

I got this error

Undefined property: stdClass::$id

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

  • 写回答

2条回答 默认 最新

  • dongzipu7517 2019-05-24 13:49
    关注

    In query select:

    ->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",
                    )
    

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

    You should add:

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100