Best Way to Print Nested Object or Array in Laravel

Hello Artisan, did you know that There are helper methods in php laravel which makes easier to retrieve items from a deeply nested object or array. In this example i will show you that with source code.

Let assume we have a $user object which contains nested data. Now what we did?

$user->team->subscription->name

 

But we can do ot like below as well

object_get($user, 'team.subscription.name', 'default_value')

 

Now again let assume we have a $user array which contains nested data. Now what we did?

$data['user']['team']['subscription']['name']

 

But we can do ot like below as well

Arr::get($data,'user.team.subscription.name', 'your_default_data')

 

Recommended: Some Laravel Best Practices Every Developer Should Know

 

Hope it can help you. 

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js