1

Consider this code:

tokenArray = [
    $this->me(),
    'access_token' => $token,
    'token_type' => 'bearer',
    'expires_in' => auth()->factory()->getTTL() * 60,
];

and the output:

enter image description here

How can I have the properties in the userobject to be spread across in the $tokenArray?

Desired output:

enter image description here

1
  • Try this, it will help you. Commented Nov 13, 2019 at 4:05

2 Answers 2

2

Merge the arrays.

$tokenArray = $this->me()->toArray() + [
    'access_token' => $token,
    'token_type' => 'bearer',
    'expires_in' => auth()->factory()->getTTL() * 60,
];

Laravel 6.x Docs - Eloquent - Serializing Models and Collections - Serializing to Array toArray

PHP Manual - Operators - Array Operators +

Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't actually work. $this->me() is basically returning auth()->userOrFail().
@NewbieCoder updated earlier ... serializing the model to array with toArray
1

Try this code:

$obj = $this->me(),

The $obj is Object variable you some changes token array then get output I think this is correct:

tokenArray = [
    'id'  => $obj['0']->id
    'name'  => $obj['0']->name
    'username'  => $obj['0']->username
    'email'  => $obj['0']->email
    'access_token' => $token,
    'token_type' => 'bearer',
    'expires_in' => auth()->factory()->getTTL() * 60,
];

Another Method:

If you can use Eloquent method:

$obj = $this->me()->get()->toArray()

print_r($obj + $tokenArray);

This is Better for the Top answer.

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.