Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在拉维尔召集一个活动_Php_Laravel - Fatal编程技术网

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,但我不确定您是否必须取出
用户
声明。