doujianwei8217 2016-12-24 07:35
浏览 73
已采纳

Laravel 5.3通知发送错误

I am trying to get into using Notifications in my Laravel 5.3 app. My first is to try and send a Welcome notification immediately after a new registration. After reading the docs and following the "What's new in Laravel 5.3" tutorial, I am getting a "Call to undefined method Illuminate\Notifications\Notification::send()" error after the record is saved.

The info below is the latest thing I have tried, but everything I try is failing. When I put the notification below the create method, I get a save, but no notification is sent. I put it in front just to see what would happen, hence the error.

Here is the Welcome:

    <?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 [
            //
            ];
        }
    }

The 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: It seems that I am not getting the User instance required. I assume it is because of the type array. I have tried to collect the new user data into the $user variable, but it now throws the error: 'Call to member function 'notify()' on array. So I guess I am still not getting the right type.

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;

}

UPDATE: I am still trying to find an instance of User. My latest attempt:

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;


}

I get the error: Undefined property: App\Notifications\WelcomeToMyApp::$id.

UPDATE... HAPPY HOLIDAYS!

I am showing the following data when I do a dd($users). I added the $data argument to the notification. I get the error:

FatalThrowableError in RegisterController.php line 66: Type error: Argument 2 passed to App\Http\Controllers\Auth\RegisterController::create() must be an instance of App\User, none given, called in /home/ubuntu/workspace/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 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

{ use 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;        
}

The parameters had to be removed from the notification class as well, but this is the code that works for this purpose.

  • 写回答

2条回答 默认 最新

  • dquoj04882 2016-12-24 09:29
    关注

    Notification facade usage require first argument as collection of notifiable user instead of giving request data you should pass users collection change below

        $user = User::create([
            'familyname' => $data['familyname'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'password' => bcrypt($data['password']),
        ]);
    
       $user->notify(new WelcomeToDStrokeTennis($data));
        return $user;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝