在 null 上调用成员函数 getEmailForPasswordReset()
Call to a member function getEmailForPasswordReset() on null
我不知道为什么在尝试重设密码时出现此错误。
我有另一个具有相同代码的项目并且它可以工作。
我注意到错误在 "resources/views/auth/emails/password.php" 文件中,它确实有一个空的 $user 变量。
奇怪的是,在调用视图的函数中,$user变量有值。
当变量传递给视图时,它失去了价值。
public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
{
// We will use the reminder view that was given to the broker to display the
// password reminder e-mail. We'll pass a "token" variable into the views
// so that it may be displayed for an user to click for password reset.
$view = $this->emailView;
**// HERE THE $user VARIABLE HAS VALUE.**
**// IN THE $view VIEW THE $user VARIABLE IS NULL**
return $this->mailer->send($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
$m->to($user->getEmailForPasswordReset());
if (! is_null($callback)) {
call_user_func($callback, $m, $user, $token);
}
});
}
已更新
这是用户模型:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Authenticatable
{
use EntrustUserTrait; // add this trait to your user model
protected $fillable = [
'name', 'email'
];
protected $hidden = [
'password',
];
}
知道了,
问题是我创建并忘记了一个 ViewComposer。
这是作曲家服务提供商:
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// Using class based composers...
View::composer(
'*', 'App\Http\ViewComposers\UserComposer'
);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
这是(经过编辑使其工作)UserComposer class:
class UserComposer
{
/**
* constructor.
*/
public function __construct()
{
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$excludedViews = ['auth.emails.password'];
if (!in_array($view->getName() , $excludedViews))
{
$view->with('user', session('user'));
}
}
}
使用 "excludedViews" 变量,我可以在除重置密码之外的所有路由中注入 "user" 变量。
我不知道为什么在尝试重设密码时出现此错误。
我有另一个具有相同代码的项目并且它可以工作。
我注意到错误在 "resources/views/auth/emails/password.php" 文件中,它确实有一个空的 $user 变量。
奇怪的是,在调用视图的函数中,$user变量有值。
当变量传递给视图时,它失去了价值。
public function emailResetLink(CanResetPasswordContract $user, $token, Closure $callback = null)
{
// We will use the reminder view that was given to the broker to display the
// password reminder e-mail. We'll pass a "token" variable into the views
// so that it may be displayed for an user to click for password reset.
$view = $this->emailView;
**// HERE THE $user VARIABLE HAS VALUE.**
**// IN THE $view VIEW THE $user VARIABLE IS NULL**
return $this->mailer->send($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) {
$m->to($user->getEmailForPasswordReset());
if (! is_null($callback)) {
call_user_func($callback, $m, $user, $token);
}
});
}
已更新
这是用户模型:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Authenticatable
{
use EntrustUserTrait; // add this trait to your user model
protected $fillable = [
'name', 'email'
];
protected $hidden = [
'password',
];
}
知道了,
问题是我创建并忘记了一个 ViewComposer。
这是作曲家服务提供商:
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// Using class based composers...
View::composer(
'*', 'App\Http\ViewComposers\UserComposer'
);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
这是(经过编辑使其工作)UserComposer class:
class UserComposer
{
/**
* constructor.
*/
public function __construct()
{
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$excludedViews = ['auth.emails.password'];
if (!in_array($view->getName() , $excludedViews))
{
$view->with('user', session('user'));
}
}
}
使用 "excludedViews" 变量,我可以在除重置密码之外的所有路由中注入 "user" 变量。