Laravel 5.3通知发送错误

问题描述:

我想在我的Laravel 5.3应用程序中使用通知。我的第一个尝试是在新注册后立即发送欢迎通知。阅读完文档并按照“Laravel 5.3的新增功能”教程,保存记录后出现"Call to undefined method Illuminate\Notifications\Notification::send()"错误。Laravel 5.3通知发送错误

下面的信息是我尝试过的最新的东西,但我尝试的所有东西都失败了。当我将通知放在create方法的下面时,我得到一个保存,但没有发送通知。我把它放在前面看看会发生什么,因此是错误。

这里是欢迎:

<?php 

    namespace App\Notifications; 

    use Illuminate\Bus\Queueable; 
    use Illuminate\Notifications\Notification; 
    use Illuminate\Contracts\Queue\ShouldQueue; 
    use Illuminate\Notifications\Messages\MailMessage; 
    use App\User; 

    class WelcomeToDStrokeTennis extends Notification 
    { 
     use Queueable; 

     protected $user; 

     /** 
     * Create a new notification instance. 
     * 
     * @return void 
     */ 
     public function __construct(User $user) 
     { 


     } 

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

     /** 
     * Get the mail representation of the notification. 
     * 
     * @param mixed $notifiable 
     * @return \Illuminate\Notifications\Messages\MailMessage 
     */ 
     public function toMail($notifiable) 
     { 
      return (new MailMessage) 
       ->line('MyApp Welcomes You.') 
       ->action('Login To MyApp',   'http://dstroketennis.com') 
       ->line('Thank you for trusting MyApp!'); 
     } 

     /** 
     * Get the array representation of the notification. 
     * 
     * @param mixed $notifiable 
     * @return array 
     */ 
     public function toArray($notifiable) 
     { 
      return [ 
      // 
      ]; 
     } 
    } 

的RegisterController:

<?php 

namespace App\Http\Controllers\Auth; 

use App\User; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Validator; 
use Illuminate\Foundation\Auth\RegistersUsers; 
use Illuminate\Notifications\Notification; 
use App\Notifications\WelcomeToMyApp; 

class RegisterController extends Controller 
{ 


    use RegistersUsers; 


    protected $redirectTo = '/home'; 


    public function __construct() 
    { 
     $this->middleware('guest'); 
    } 


    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'familyname' => 'required|max:255|unique:users', 
      'email' => 'required|email|max:255|unique:users', 
      'phone' => 'required|unique:users', 
      'password' => 'required|min:6|confirmed', 
     ]); 
    } 


    protected function create(array $data) 
    { 
     Notification::send($data, new WelcomeToDStrokeTennis($data)); 

     return User::create([ 
      'familyname' => $data['familyname'], 
      'email' => $data['email'], 
      'phone' => $data['phone'], 
      'password' => bcrypt($data['password']), 
     ]); 

    } 
} 

UPDATE: 看来,我没有得到所需要的用户实例。我认为这是因为类型数组。我试图将新用户数据收集到$ user变量中,但它现在会抛出错误: '调用成员函数'notify()'。所以我想我还没有得到正确的类型。

protected function create(array $data) 
{ 
    $user = collect(User::create([ 
     'familyname' => $data['familyname'], 
     'email' => $data['email'], 
     'phone' => $data['phone'], 
     'password' => bcrypt($data['password']), 
    ]))->all(); 

    $user->notify(new WelcomeToMyApp($user)); 

    return $user; 

} 

更新: 我仍然在试图找到用户的一个实例。我的最新尝试:

protected function create(array $data) 
{ 
     User::create([ 
     'familyname' => $data['familyname'], 
     'email' => $data['email'], 
     'phone' => $data['phone'], 
     'password' => bcrypt($data['password']), 
    ]); 
    $user = User::orderBy('created_at', 'desc')->first(); 
    $user->notify(new WelcomeToMyApp($user)); 
    return $user; 


} 

我收到错误:未定义的属性:App \ Notifications \ WelcomeToMyApp :: $ id。

更新...快乐假日!

我在做dd($ users)时显示以下数据。我在通知中添加了$ data参数。我得到的错误:

FatalThrowableError在RegisterController.php线66: 类型错误:传递给应用程序\的Http参数2 \ \控制器验证\ RegisterController ::创建()必须应用\ User实例,没有给出被称为在/home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php上线33

dd($user)

protected function create(array $data, User $user) 
{ 
     User::create([ 
     'familyname' => $data['familyname'], 
     'email' => $data['email'], 
     'phone' => $data['phone'], 
     'password' => bcrypt($data['password']), 
    ]); 
    $user = User::orderBy('created_at', 'desc')->first(); 
    //dd($user); 
    $user->notify(new WelcomeToMyApp($data)); 
    return $user; 

} 

class WelcomeToDStrokeTennis extends Notification 

{ 使用QUEUEABLE;

protected $user; 
protected $data; 

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

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

/** 
* Get the mail representation of the notification. 
* 
* @param mixed $notifiable 
* @return \Illuminate\Notifications\Messages\MailMessage 
*/ 
public function toMail($notifiable) 
{ 
    return (new MailMessage) 
       ->subject('MyApp Welcomes You.') 
       ->greeting('You are now a registered user.') 
       ->line('If you have not done so, please login and enter your player profile information') 
       ->action('Login To D`Stroke Tennis', 'http://dstroketennis.com') 
       ->line('Please call, text, or email if you have any problems or questions.'); 
} 

/** 
* Get the array representation of the notification. 
* 
* @param mixed $notifiable 
* @return array 
*/ 
public function toArray($notifiable) 
{ 
    return [ 
     // 
    ]; 
} 

}

SOLUTION:

protected function create(array $data) 
{ 
    $user = User::create([ 
     'familyname' => $data['familyname'], 
     'email' => $data['email'], 
     'phone' => $data['phone'], 
     'password' => bcrypt($data['password']), 
    ]); 

    $user->notify(new WelcomeToMyApp($user)); 
    return $user;   
} 

的参数必须从通知类中删除,以及,但是这是为了这个目的工作的代码。

通知门面使用要求第一个参数是法定的用户而不是给请求数据的采集应通过用户低于

$user = User::create([ 
     'familyname' => $data['familyname'], 
     'email' => $data['email'], 
     'phone' => $data['phone'], 
     'password' => bcrypt($data['password']), 
    ]); 

    $user->notify(new WelcomeToDStrokeTennis($data)); 
    return $user; 
+0

这对我有意义,但我得到一个错误,说: – wdarnellg

+0

类型错误:参数1传递给应用程序\通知\ WelcomeToDStrokeTennis :: __构造()必须是应用程序\用户,实例的数组给出,调用/ home /ubuntu/workspace/app/Http/Controllers/Auth/RegisterController.php上线75 – wdarnellg

+0

更改通知构造和参数删除用户实例,而不是让它像数组$数据=阵列() –

这可能是因为Notification是一个门面。 尝试使用

use Notification; 

相反的,

use Illuminate\Notifications\Notification; 
+0

我收集的变化对与回复不同步表示歉意。我做了更改以使用通知;我继续得到$ user是一个数组的引用。我以为我可以使用'Collections'获取新的用户实例,但是错误更改为'调用数组'的成员函数notify()。 – wdarnellg