Php 在拉维尔召集一个活动
我这样称呼一个事件:Php 在拉维尔召集一个活动,php,laravel,Php,Laravel,我这样称呼一个事件: event(new NewsLetterActivation($user)); 并且在事件中: public $user; public function __construct(User $user) { $this->user = $user; } 它起作用了 有时,我有一个额外的字符串变量,也可能是我没有的 $user($user为空或null) 我怎么称呼它 我试试这个: event(new NewsLetterActivation(null,'a
event(new NewsLetterActivation($user));
并且在事件中:
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
它起作用了
有时,我有一个额外的字符串变量,也可能是我没有的
$user($user为空或null)
我怎么称呼它
我试试这个:
event(new NewsLetterActivation(null,'a@a.com'));
如果:
public function __construct(User $user,$email=null)
{
$this->user = $user;
$this->email= $email;
}
错误:
Type error: Argument 1 passed to App\Events\NewsLetterActivation::__construct() must be an instance of App\Events\User, null given
您可以这样做,以便能够传递null
:
public function __construct(?User $user)
将控制器更改为以下设置:-
public function __construct($user,$email=null)
{
$this->user = $user;
$this->email= $email;
}
在\u construct()
方法或侦听器中,必须更改输入参数:
public function __construct(array $arg)
{
if (isset($agr['user])) {
$this->user = $agr['user];
}
if (isset($arg['email'])) {
$this->email= $agr['email'];
}
}
然后,您可以通过不同的方式调用事件:
event(new NewsLetterActivation(['email'=>'a@a.com']);
event(new NewsLetterActivation(['email'=>'a@a.com', 'user'=>$user]);
event(new NewsLetterActivation(['user'=>$user]);
event(new NewsLetterActivation([]);
这意味着你必须传入一个用户,不能传入null。您可以将其赋值为null,但我不确定您是否必须取出用户
声明。