2016-12-24 20 views
0

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; 

} 

UPDATE: 私はまだユーザーのインスタンスを見つけようとしています。私の最新の試み:

私はエラーが発生します:未定義のプロパティ:App \ Notifications \ WelcomeToMyApp :: $ id。

更新...幸せな休日!

私はdd($ users)を実行すると次のデータを表示しています。 $ data引数を通知に追加しました。

FatalThrowableError in RegisterController.php 66行目: 型エラー:App \ Http \ Controllers \ Auth \ RegisterController :: create()に渡される引数2は、App \ 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 [ 
     // 
    ]; 
} 

}

解決

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

パラメータが同様に通知クラスから除去しなければならなかったが、これは、この目的のために動作するコードです。

答えて

1

通知ファサードの使用量が代わりに要求データを与えるの届出ユーザーのコレクションとして最初の引数を必要とし、その代わりの

use Notification; 

を使用してみてください、あなたは、ユーザーが

$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がApp \ Notifications \ WelcomeToDStrokeTennis :: __ construct()に渡されている必要があります。 /bin/uubuntu/workspace/app/Http/Controllers/Auth/RegisterController.php on line 75 – wdarnellg

+0

通知コンストラクタを変更して、引数からUserインスタンスを削除する代わりに、$ data = array()のように配列してください –

0

Notificationがファサードである可能性があります。

use Illuminate\Notifications\Notification; 
+0

I下記のコレクションの変更を渡す必要があります返信と同期がとれていないことをお詫びします。私は通知を使用するように変更しました。私は、$ userが配列であるというリファレンスを引き続き得ました。私は、 'Collections'を使用して新しいユーザーインスタンスを取得できると思ったが、エラーが '配列のメンバ関数notify()を呼び出す'に変更されました。 – wdarnellg

関連する問題