Laravel 5.3 通知发送错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41311336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 Notification Send Error
提问by wdarnellg
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.
我正在尝试在我的 Laravel 5.3 应用程序中使用通知。我的第一个方法是尝试在新注册后立即发送欢迎通知。阅读文档并遵循“Laravel 5.3 中的新功能”教程后,我在"Call to undefined method Illuminate\Notifications\Notification::send()"
保存记录后收到错误消息。
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.
下面的信息是我尝试过的最新信息,但我尝试的一切都失败了。当我将通知放在 create 方法下方时,我得到了保存,但没有发送通知。我把它放在前面只是为了看看会发生什么,因此出现错误。
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.
更新:似乎我没有得到所需的用户实例。我认为这是因为类型数组。我试图将新用户数据收集到 $user 变量中,但它现在抛出错误:'Call to member function 'notify()' on array。所以我想我仍然没有得到正确的类型。
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:
更新:我仍在尝试查找 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;
}
I get the error: Undefined property: App\Notifications\WelcomeToMyApp::$id.
我收到错误:未定义的属性: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:
我在执行 dd($users) 时显示以下数据。我在通知中添加了 $data 参数。我收到错误:
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
在 RegisterController.php 第 66 行中出现 FatalThrowableError:类型错误:传递给 App\Http\Controllers\Auth\RegisterController::create() 的参数 2 必须是 App\User 的一个实例,没有给出,在 /home/ubuntu/workspace/ 中调用第 33 行的 vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php
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.
参数也必须从通知类中删除,但这是用于此目的的代码。
采纳答案by Sagar Rabadiya
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;
回答by Akshay Gohil
This might be because Notification
is a facade.
Try using
这可能是因为Notification
是外观。尝试使用
use Notification;
Instead of,
代替,
use Illuminate\Notifications\Notification;