ziaakbari's avatar

How to access array inside an object?

Hi there, I have a function in my controller like bellow

	public function notifyShipper(){
        	$job = Job::with('shipper', 'jobstatus')->find($id);
        	return $job;
	}

returned object in console is like bellow

 {
	....
	....
	shipper: {
		addresses:{
			name: "Ali"
			src_des: [
				{
				  	id: 21,
           			  	company: "zia",
            			  	address: "some fake address",
				},
				{
				  	id: 22,
           			  	company: "xia",
            			  	address: "some fake address",
				}
			]
		}
	}
}

if I return $job->shipper->addresses->name, I get "Ali", but if I return $job->shipper->addresses->src_des, I get nothing

0 likes
4 replies
Sinnbeck's avatar

I assume that it is a collection

$job->shipper->addresses->src_des->first();
ziaakbari's avatar

It looks src_des property doesn't exist in this object. I see the same result when I return $job->shipper->addresses->xyz, wheres xyz doesn't exist.

Sinnbeck's avatar

Do you get it also if you inspect it using dd?

dd($job->toArray());
CodeCanyon's avatar

@ziaakbari

Can you try a print_r() or var_dump to see the results for $job->shipper->addresses->src_des ?

If that's also not showing anything, try json_encode() and json_decode() ?

Please or to participate in this conversation.