i have a lumen project that is using mongo db now i want to access the data and do some logic on them my object in mongo is like below :
_id: ObjectId('602cfb30bc865100073f0e56'),
serviceType: 'normal',
segment: 'Basic',
steps: {
'step1': 1,
'step2': 2
}
nnow in my laravel application i do it like below :
$data = DiscountRule::first();
dd($data);
and the result is like below :
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 => "*"
]
}
now it returns the data as array
but i want to return as object so that i can for example use $data->segment
and get the data in segment . any idea how can i return data from mongo as object ?
As stated in the first line of your dd()
. The type of the return is App\Models\Rule\DiscountRule
. All the underlying arrays, is how Laravel
models works internally with setting attributes.
So you should be able to do.
$rule = DiscountRule::first();
dd($rule->segment);