I'm trying to get the logged in users details by sending the jwt into the headers, but I also want to get the user's details in another table. I want to see the user's details plus the user's post in another table as well. This the function
public function getAuthenticatedUser()
{
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['token_expired'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['token_invalid'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['token_absent'], $e->getStatusCode());
}
// the token is valid and we have found the user via the sub claim
$user = auth()->user();
$userid = User::where('id', $user->id)->first();
$return = UserResource::collection($userid);
return response()->json($return);
}
This is the error message
<!--
BadMethodCallException: Call to undefined method App\User::mapInto() in file C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php on line 50
#0 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php(36): Illuminate\Database\Eloquent\Model::throwBadMethodCallException('mapInto')
#1 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1620): Illuminate\Database\Eloquent\Model->forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'mapInto', Array)
#2 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Http\Resources\CollectsResources.php(30): Illuminate\Database\Eloquent\Model->__call('mapInto', Array)
#3 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Http\Resources\Json\ResourceCollection.php(52): Illuminate\Http\Resources\Json\ResourceCollection->collectResource(Object(App\User))
#4 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Http\Resources\Json\AnonymousResourceCollection.php(25): Illuminate\Http\Resources\Json\ResourceCollection->__construct(Object(App\User))
#5 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Http\Resources\Json\JsonResource.php(78): Illuminate\Http\Resources\Json\AnonymousResourceCollection->__construct(Object(App\User), 'App\\Http\\Resour...')
#6 C:\xampp\htdocs\doc\app\Http\Controllers\getUserController.php(67): Illuminate\Http\Resources\Json\JsonResource::collection(Object(App\User))
#7 [internal function]: App\Http\Controllers\getUserController->getAuthenticatedUser()
#8 C:\xampp\htdocs\doc\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): call_user_func_array(Array, Array)
While this is the App\\User (user model)
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Auth\Passwords\CanResetPassword;
use Auth;
class User extends Authenticatable implements MustVerifyEmail, JWTSubject
{
use Notifiable;
public function __construct()
{
Auth::shouldUse('users');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'phone', 'password', 'Age', 'Blood', 'Gender', 'Height', 'Weight', 'picture', 'history', 'record',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
public function appointment(){
return $this->hasMany('App\Appointment');
}
}
What is the possible fix?
Take a look at the documentation about resource collections.
You can see they are using User::all()
. You on the other hand are using User::where('id', $user->id)->first();
. The difference between those is the return value. all()
returns a Collection
, first()
returns (in this case) a single User
instance.
The error you are getting already tells you what is wrong. "Call to undefined method App\\User::mapInto()". It tries to call mapInto
on a User
. However mapInto
is a method of Collection
.
Knowing this we could do things depening what you want.
Do you want to return multiple users?
UserResource::collection(User::all());
Do you want to return one user?
// You already have user from auth()->user(). No need to refetch it.
new UserResource(auth()->user());
For adding the posts of the user I suggest you take a look at eager loading a relationship and conditionally adding it to the UserResource
.
Eager loading and conditionally adding a relationship are not required in this scenario, but I highly suggest to do this guessing you might use the UserResource
also for returning multiple User
instances.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.