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

NewbieCoder
  • 676
  • 1
  • 9
  • 32
  • Try [this](https://stackoverflow.com/a/6785366/11988937), it will help you. –  Nov 13 '19 at 04:05

2 Answers2

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 +

lagbox
  • 48,571
  • 8
  • 72
  • 83
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.

lagbox
  • 48,571
  • 8
  • 72
  • 83