【发布时间】:2021-05-20 08:40:31
【问题描述】:
我有一个使用 mongo db 的 lumen 项目,现在我想访问数据并对它们执行一些逻辑,我在 mongo 中的对象如下所示:
_id: ObjectId('602cfb30bc865100073f0e56'),
serviceType: 'normal',
segment: 'Basic',
steps: {
'step1': 1,
'step2': 2
}
现在在我的 laravel 应用程序中我会这样做:
$data = DiscountRule::first();
dd($data);
结果如下:
App\Models\Rule\DiscountRule {#183
#collection: "discountRules"
#connection: "mongodb"
#dispatchesEvents: array:2 [
"saved" => "App\Utility\Mongo\Listeners\ModelSaved"
"deleted" => "App\Utility\Mongo\Listeners\ModelDeleted"
]
-events: []
#primaryKey: "_id"
#keyType: "string"
#parentRelation: null
#table: "discountRules"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [
"_id" => MongoDB\BSON\ObjectId {#150
+"oid": "602cfb30bc865100073f0e56"
}
"serviceType" => "pishropost-regular"
"segment" => "Basic"
"steps" => array:2 [
"itemCount > 0 and itemCount <= 9" => 60000
"itemCount > 9" => 0
]
]
#original: array:4 [
"_id" => MongoDB\BSON\ObjectId {#150}
"serviceType" => "pishropost-regular"
"segment" => "Basic"
"steps" => array:2 [
"itemCount > 0 and itemCount <= 9" => 60000
"itemCount > 9" => 0
]
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
现在它以array 的形式返回数据,但我想以对象的形式返回,这样我就可以使用$data->segment 并获取段中的数据。知道如何从 mongo 作为对象返回数据吗?
【问题讨论】: