check authentication on Laravel schedule

让人想犯罪 __ 提交于 2019-12-04 04:37:24

问题


I have schedule like this

$schedule->call(function () {
     $mial->getMail(Auth::user()->id);
})->everyMinute();

show me this error

In Kernel.php line 37:

Trying to get property of non-object

When I run this command show me this error again

$schedule->call(function () {
    echo Auth::user()->id;
})->everyMinute();

I need to be check authentication.


回答1:


You can't do that because a user doesn't run the scheduled command, so auth()->user() object will always be null.

To fix this, you can save user ID in DB and fetch the data on scheduled command execution.




回答2:


You cannot check auth via the command line as a command does not have any information about the session. It is not concerned with HTTP / a browser at all



来源:https://stackoverflow.com/questions/47975301/check-authentication-on-laravel-schedule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!