0

I'd like to get a specific array data from an array I've get from this code :

$user       = $request->instance()->query('user');
var_dump($user);exit;

And the result is :

array(1) { ["user"]=> object(App\Models\User)#332 (27) { ["fillable":protected]=> array(5) { [0]=> string(4) "name" [1]=> string(8) "username" [2]=> string(5) "email" [3]=> string(8) "password" [4]=> string(5) "token" } ["hidden":protected]=> array(2) { [0]=> string(8) "password" [1]=> string(14) "remember_token" } ["casts":protected]=> array(1) { ["email_verified_at"]=> string(8) "datetime" } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(5) "users" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(15) { ["id"]=> int(3) ["name"]=> string(14) "Manchesteriyah" ["username"]=> string(14) "manchesteriyah" ["email"]=> string(24) "[email protected]" ["facebook"]=> string(37) "https://web.facebook.com/xkionna" ["phone"]=> NULL ["email_verified_at"]=> NULL ["password"]=> string(60) "$2y$10$IrqHE1JfyH0bJ0XD/Hjy.efLg95y/buTIir0PuXcOqDb1yCSS69Oe" ["image"]=> NULL ["description"]=> NULL ["role"]=> string(1) "1" ["token"]=> string(20) "ymsJxEtFnxdPBWYwlYFw" ["member_expiration"]=> string(19) "2019-07-08 20:33:29" ["created_at"]=> string(19) "2019-06-08 20:30:25" ["updated_at"]=> string(19) "2019-06-08 20:33:29" } ["original":protected]=> array(15) { ["id"]=> int(3) ["name"]=> string(14) "Manchesteriyah" ["username"]=> string(14) "manchesteriyah" ["email"]=> string(24) "[email protected]" ["facebook"]=> string(37) "https://web.facebook.com/xkionna" ["phone"]=> NULL ["email_verified_at"]=> NULL ["password"]=> string(60) "$2y$10$IrqHE1JfyH0bJ0XD/Hjy.efLg95y/buTIir0PuXcOqDb1yCSS69Oe" ["image"]=> NULL ["description"]=> NULL ["role"]=> string(1) "1" ["token"]=> string(20) "ymsJxEtFnxdPBWYwlYFw" ["member_expiration"]=> string(19) "2019-07-08 20:33:29" ["created_at"]=> string(19) "2019-06-08 20:30:25" ["updated_at"]=> string(19) "2019-06-08 20:33:29" } ["changes":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["rememberTokenName":protected]=> string(14) "remember_token" } }

What I'd like to get is the username value, I've tried some solution like this :

$user->username

but it give me error like this :

Trying to get property 'username' of non-object

How to fix this? Thanks for attention.

1 Answer 1

2

The $user variable seems to be holding an array containing 1 element, not the User itself. You must first retrieve the User object from the array and then access the username attribute.

$user = $request->instance()->query('user')['user'];
var_dump($user->username); exit;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.