我已尝试在Cron作业中获取当前用户ID以安排任务,但其说尝试获取非对象的属性
public function handle()
{
$data = MenterRequest::where('mentor_provider_id','=',Auth::user()->id)->first();
$this->info('Deleted request successfully');
}
现在我可以获得当前的用户数据。
答案 0 :(得分:1)
使用标准的php函数怎么样:getmyuid()
?
然后你的代码将成为:
$data = MenterRequest::where('mentor_provider_id','=',getmyuid());
答案 1 :(得分:1)
构造作业时,将用户(或仅用户ID)作为构造函数的参数传递给
$theJob = new SomeJob(\Auth::user());
你的工作......
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$data = MenterRequest::where('mentor_provider_id','=',$this->user->id)->first();
$this->info('Deleted request successfully');
}
如果您在User和MentorRequest之间建立了正确的关系,那么您可以做一些更漂亮的事情,比如
public function handle()
{
$data = $this->user->mentor_requests->first()->delete();
$this->info('Deleted request successfully');
}
甚至更好 - 您可以将用户模型上的mentor_request的删除封装在一个重用的函数中(例如称为deleteMentorRequest()
),然后执行
public function handle()
{
$this->user->deleteMentorRequest();
$this->info('Deleted request successfully');
}