laravel 调用未定义的方法 Illuminate\Notifications\Notification::send()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43706229/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:53:27  来源:igfitidea点击:

Call to undefined method Illuminate\Notifications\Notification::send()

laravellaravel-5laravel-5.4laravel-notification

提问by Mohamed Wannous

I am trying to make a notification system in my project.
These are the steps i have done:

我正在尝试在我的项目中制作一个通知系统。
这些是我已经完成的步骤:

1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost

1-php artisan 通知:表
2-php artisan migrate
3-php artisan make:notification AddPost

In my AddPost.phpfile i wrote this code:

在我的AddPost.php文件中,我写了这段代码:

<?php

namespace App\Notifications;

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

class AddPost extends Notification
{
    use Queueable;


    protected $post;
    public function __construct(Post $post)
    {
        $this->post=$post;
    }


    public function via($notifiable)
    {
        return ['database'];
    }




    public function toArray($notifiable)
    {
        return [
            'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
        ];
    }
}

In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:

在我的控制器中,我试图将数据保存在表中,一切都很完美。
这是我的控制器中的代码:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;

class PostNot extends Controller
{
    public function index(){
       $posts =DB::table('_notification')->get();
       $users =DB::table('users')->get();
       return view('pages.chat',compact('posts','users'));


    }
public function create(){

        return view('pages.chat');

    }


public function store(Request $request){
    $post=new Post();
   //dd($request->all());
   $post->title=$request->title;
   $post->description=$request->description;
   $post->view=0;

   if ($post->save())
   {  
    $user=User::all();
    Notification::send($user,new AddPost($post));
   }

   return  redirect()->route('chat');  
    }

}

Everything was good until I changed this code:

一切都很好,直到我更改了此代码:

$post->save();

to this :

对此:

if ($post->save())
       {  
        $user=User::all();
        Notification::send($user,new AddPost($post));

       }

It started to show an error which is:

它开始显示一个错误,即:

FatalThrowableError in PostNot.php line 41: Call to undefined method Illuminate\Notifications\Notification::send()

PostNot.php 第 41 行中的 FatalThrowableError:调用未定义的方法 Illuminate\Notifications\Notification::send()

How can i fix this one please??
Thanks.

请问这个怎么修啊??
谢谢。

回答by Marcin Nabia?ek

Instead of:

代替:

use Illuminate\Notifications\Notification;

you should use

你应该使用

use Notification;

Now you are using Illuminate\Notifications\Notificationand it doesn't have sendmethod and Notificationfacade uses Illuminate\Notifications\ChannelManagerwhich has sendmethod.

现在您正在使用Illuminate\Notifications\Notification并且它没有send方法,而Notification外观使用Illuminate\Notifications\ChannelManager具有send方法。