Custom payload with Laravel JWT
The way I add custom payload in my controller:
$customClaims = ['foo' => 'bar', 'baz' => 'bob'];
$token = JWTAuth::claims($customClaims)->attempt($credentials);
The way I get back my custom payload:
dd(auth()->payload()->get('foo'));
Try below code is working for me.
//load user place your code for load user
$user = User::find( $user_id );
// if you don't have user id then also you can used.
$user = User::where( 'email', $tokenPayload->email )->first();
$payloadable = [
'id' => $tokenPayload->id,
'name' => $tokenPayload->name,
'email' => $tokenPayload->email,
'deleted_at' => $tokenPayload->deleted_at,
'created_at' => $tokenPayload->created_at,
'updated_at' => $tokenPayload->updated_at,
'organization' => $request->organization_id
];
$token = JWTAuth::fromUser($user,$payloadable);
you can get organization using below code.
$payload = JWTAuth::parseToken()->getPayload();
// then either of
$payload->get('organization');
you can get new token using fromUser
method by passing the user object.
try this code I hope this code is working for you.
You can get more detail from here.