1 month ago

Problem with registering user through service with Passport in Laravel

Posted 1 month ago by dr24

I am building registration form with Passport in Laravel using repository pattern, and when I try to register with postman I get error

Undefined property: Illuminate\Http\Response::$id

It breaks when I try and return response like this

return response(['user' => $user, 'access_token' => $accessToken]);

but when I just return $user instead then it works fine, but I don't have access token then. How can I register succesfully with access token? Any help is appreciated. Here is my code.


public function register(RegisterUserRequest $request)
    try {
        $user = $this->service->createNewUser($request);
        return $this->returnResource($user);
    } catch (\Exception $exception) {
        return $this->failureJson($exception);


public function createNewUser(BaseRequest $request)
    try {
        $request['confirmation_hash'] = Str::random(50);
        $request['password'] = Hash::make($request['password']);

        $user = parent::create($request);

        Mail::to($user['email'])->send(new ConfirmationMail($user)); // Send email to user to confirm his email!

        $accessToken = $user->createToken('authToken')->accessToken;


        return response(['user' => $user, 'access_token' => $accessToken]);  // HERE IT BREAKS!!!
    } catch (\Exception $exception) {

        throw new \Exception($exception->getMessage());


public function toArray($request)
    return [
        'id' => $this->id,
        'name' => $this->name,
        'first_name' => $this->first_name,
        'last_name' => $this->last_name,
        'email' => $this->email,
        'password' => $this->password,
        'remember_token' => $this->remember_token,
        'api_token' => $this->api_token,
        'confirmation_hash' => $this->confirmation_hash,
        'email_verified_at' => $this->email_verified_at,
        'is_verified' => $this->is_verified,
        'verification_expires_at' => $this->verification_expires_at

Please sign in or create an account to participate in this conversation.