Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Call to a member function forceFill() on null" Error when calling $user->createToken('Foo')->accessToken #713

Closed
tomothumb opened this issue May 17, 2018 · 19 comments
Labels

Comments

@tomothumb
Copy link

Same issue as the one below link.
https://laracasts.com/discuss/channels/laravel/call-to-a-member-function-forcefill-on-null-error-when-calling-user-createtokentoken-accesstoken

This happens sometimes. It's not every time.
Do you have any idea of reason and solution?

/www/vendor/laravel/passport/src/PersonalAccessTokenFactory.php

    public function make($userId, $name, array $scopes = [])
    {
        $response = $this->dispatchRequestToAuthorizationServer(
            $this->createRequest($this->clients->personalAccessClient(), $userId, $scopes)
        );
 
        $token = tap($this->findAccessToken($response), function ($token) use ($userId, $name) {
            $this->tokens->save($token->forceFill([
                'user_id' => $userId,
                'name' => $name,
            ]));
        });
 
        return new PersonalAccessTokenResult(
            $response['access_token'], $token
        );
    }
@driesvints
Copy link
Member

Closing this issue because it's already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue.

@Ravi1990
Copy link

Mention fix is already in new laravel update but i am still facing this issue.
When i try multiple time it works few time but most of the time it give following error:
{
"message": "Call to a member function forceFill() on null",
"exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
"file": "/var/app/current/vendor/laravel/passport/src/PersonalAccessTokenFactory.php",
"line": 75,
"trace": [
{
"file": "/var/app/current/vendor/laravel/framework/src/Illuminate/Support/helpers.php",
"line": 1041,
"function": "Laravel\Passport{closure}",
"class": "Laravel\Passport\PersonalAccessTokenFactory",
"type": "->"
},
{
"file": "/var/app/current/vendor/laravel/passport/src/PersonalAccessTokenFactory.php",
"line": 79,
"function": "tap"
},

@driesvints driesvints added the bug label Oct 29, 2018
@driesvints
Copy link
Member

I'll try to look at this at a later time. In the meantime feel free to check if you can provide a failing test.

@driesvints driesvints reopened this Oct 29, 2018
@Qlwentt
Copy link

Qlwentt commented Oct 29, 2018

what laravel update is supposed to fix this? I'm having the same issue. I'm on laravel 5.6.39

@Ravi1990
Copy link

Ravi1990 commented Oct 30, 2018

Yes i have also laravel 5.6.39, same issue here. The solution mention above is this #467. But you look in vendor TokenRepository, mention fix is already there, but issue still persist.
In my code on login it generate token and provide access, now user has to hit login 2-3 times to get into login. Because it work after some try.

@Ravi1990
Copy link

Also i like to mention here that it is working fine in my local system, issue occurs on live server on production. May be some issue with timing, production server is fast so it try to executing something which is not initialize may be.

@Qlwentt
Copy link

Qlwentt commented Oct 30, 2018

Yes, same here. this error is only happening to me on a production server about 15-20 times per day. It does not happen every single time a user tries to log in, only intermittently.

@Ravi1990
Copy link

Ravi1990 commented Nov 2, 2018

I'll try to look at this at a later time. In the meantime feel free to check if you can provide a failing test.

Any Update on this?

@driesvints
Copy link
Member

@Ravi1990 still haven't found time yet. Can you please try upgrading to Laravel 5.7 and the latest Passport version and see if the problem persists?

@Ravi1990
Copy link

Ravi1990 commented Dec 3, 2018

@Ravi1990 still haven't found time yet. Can you please try upgrading to Laravel 5.7 and the latest Passport version and see if the problem persists?

upgraded to Laravel 5.7 still same issue.

@Ravi1990
Copy link

Ravi1990 commented Dec 6, 2018

Hi, i have found the issue it was because of Read & Write Connections i am using and was not using " 'sticky'=>true", so it write from one connection and read from another connection which was not updated as it was happening so fast on server.
So it gives "forceFill() on null" as $token is not found on read connection. So the solution for me was to use 'sticky'=>true in database config.

@driesvints
Copy link
Member

@Ravi1990 okay

Please @Qlwentt let me know if this was also the case for you.

@Qlwentt
Copy link

Qlwentt commented Jan 11, 2019

Sorry for the late reply! Yes that was the same issue me

@loktarjugg
Copy link

same issue here
i may have found the cause of the problem, before creating the token, I deleted the old token

now i delete this code and it works.
image

@yogeshkoli
Copy link

I also had the error on Laravel 5.6 using MySQL 5.7. so to describe main reason behind this cause is Read & Write Connections.
Solution: Add 'sticky' => true, to you database.php config file. and you are good to go.

@MohitVisions
Copy link

My file have already 'sticky' => true but still show same error i am using laravel 6 .

@tsaini530
Copy link

tsaini530 commented Sep 2, 2020

I added 'sticky' => true in my databse.php file but still, show the same error. I am using Laravel 7.21 and Postgres.
This happens sometimes. It's not every time. only 2 users face this problem out of 10

@PhamBaTrungThanh
Copy link

I'm on Laravel 5.4 and MySQL 5.7 and this error still occur, but very rarely (like once/twice a month).

Does anyone have the explaination for this?

@jesiljose-bridge
Copy link

Same issue here as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants