Laravel: Auth::user()在自定义控制台命令中返回null



我想在laravel中发送通知,我有一个问题。我搜索了答案,但我没有找到任何东西。我的代码看起来像这样:

<?php

namespace AppNotifications;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
use AppModelsAttribute;

class ExpiredDateNotification extends Notification
{
use Queueable;

public $attribute;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($attribute)
{
$this->attribute = $attribute;
}

/**
* Get the notification's delivery channels.
*
* @param  mixed  $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}

/**
* Get the mail representation of the notification.
*
* @param  mixed  $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('You must to renew your contract!')
->line('Thank you for using our services!');
}

/**
* Get the array representation of the notification.
*
* @param  mixed  $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'name' => $this->attribute->attribute_name,
'expired_at' => $this->attribute->expired_at
];
}
}


<?php

namespace AppConsole;

use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
use AppModelsUser;
use AppNotificationsExpiredDateNotification;
use AppModelsAttribute;
use AppConsoleCommandsSendNotification;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
SendNotification::class
];

/**
* Define the application's command schedule.
*
* @param  IlluminateConsoleSchedulingSchedule  $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('command:SendNotification');
}

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}



<?php

namespace AppConsoleCommands;

use IlluminateConsoleCommand;
use AppModelsUser;
use AppHttpControllersNotificationController;
use IlluminateSupportFacadesAuth;
use CarbonCarbon;
use AppNotificationsExpiredDateNotification;
use DB;
use Notification;

class SendNotification extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:SendNotification';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send notification on email when an attribute is set to expire';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/

public function handle()
{
$users = auth()->user();;

$attributes[] = User::join('object_models', 'object_models.user_id', '=', 'users.id')->
join('attributes', 'attributes.object_id', '=', 'object_models.id')->
where('attributes.expired_at', '<', Carbon::now()->addDays(7))->
where('users.id', '=', $user->id)->
get();



$user->notify(new ExpiredDateNotification($attribute));

}
}

但是这里在句柄函数我的$user = auth()->user()是NULL…在过去的几个小时里,我在谷歌上搜索了解决方案,但我不知道如何解决,如果你有提示,请告诉我。谢谢你

除非您从请求生命周期的任何部分(例如,在用户登录之后)触发通知工作流,否则authhelper无法从任何地方拉出经过身份验证的用户。

对于您所展示的用例,您必须自己提供它,例如作为命令参数:

protected $signature = 'command:SendNotification
{user_id : id of the recipient model}';
public function handle()
{
$user_id = $this->argument('user_id');
$user = User::findOrFail($user_id);
// other commands
}

所以执行php artisan command:SendNotification 1就像用户1被认证了一样。

找出过期属性的逻辑(如果有的话)似乎超出了这个问题的范围,所以我建议您从使用虚拟属性检查它是否有效开始;

public function handle()
{
$user_id = $this->argument('user_id');
$user = User::findOrFail($user_id);
$attribute = (object) [
'name'=>'dummyAttribute', 
'expired_at'=>now()->->addDays(7)
];
$user->notify(new ExpiredDateNotification($attribute));
}

相关内容

  • 没有找到相关文章

最新更新