调用未定义的方法stdClass :: notify()

时间:2017-04-22 17:57:10

标签: php laravel

MyController中的FatalThrowableError 调用未定义的方法stdClass :: notify()

laravel notify()未定义的方法。如何解决它....帮助我..

控制器文件:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Mail;
use Apps\User;
use App\Notifications\InvoicePaid;

class BlogController extends Controller
{
  public function EmailNotify(){
  $user = DB::table('users')->where('id',2)->first();
  $urlData = DB::table('url')->where('id',2)->first();

  $user->notify(new InvoicePaid($urlData));

 }
}

应用程序/通知/ InvoicePaid.php

namespace App\Notifications;

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

class InvoicePaid extends Notification
{
 use Queueable;
 protected $toto;

public function __construct($toturial)
{
    $this->toto=$toturial;
}

public function via($notifiable)
{
    return ['mail'];
}
public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!');
  }

}

1 个答案:

答案 0 :(得分:3)

要使用notify方法,您需要将Notifiable特征添加到您的班级。

class User extends Authenticatable {
    use \Illuminate\Notifications\Notifiable;

    //...
}

如果您不想使用Notifiable特质,可以使用Notification外观。

Notification::send($users, new InvoicePaid($invoice));

有更多详细信息here。 Laracasts也有一个视频。 Watch it here