【问题标题】:Call to undefined method Illuminate\Notifications\Notification::send()调用未定义的方法 Illuminate\Notifications\Notification::send()
【发布时间】:2017-09-28 02:36:16
【问题描述】:

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

1-php 工匠通知:表格
2-php 工匠迁移
3-php artisan make:notification AddPost

在我的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
        ];
    }
}

在我的控制器中,我试图将数据保存在表格中,并且每件事都很完美。
这是我在控制器中的代码:

<?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');  
    }

}

在我更改此代码之前一切都很好:

$post->save();

到这里:

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

       }

它开始显示错误是:

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

请问我该如何解决这个问题??
谢谢。

【问题讨论】:

    标签: laravel laravel-5 laravel-5.4 laravel-notification


    【解决方案1】:

    代替:

    use Illuminate\Notifications\Notification;
    

    你应该使用

    use Notification;
    

    现在您使用的是Illuminate\Notifications\Notification,它没有send 方法,而Notification 外观使用Illuminate\Notifications\ChannelManager,它有send 方法。

    【讨论】:

      【解决方案2】:

      使用这个 use Illuminate\Support\Facades\Notification;

      而不是这个 use Illuminate\Notifications\Notification;

      为我解决了问题。

      希望这对某人有所帮助。

      【讨论】:

      • 谢谢!使用 Facades 路径为我解决了类似的问题。
      【解决方案3】:

      使用这个更好

      使用通知

      代替

      使用 Illuminate\Support\Facades\Notification

      这使得 send() 无法访问 [#Notification Databse]

      【讨论】:

        猜你喜欢
        • 2021-09-01
        • 2023-04-07
        • 2018-08-14
        • 2023-03-22
        • 2018-03-29
        • 2015-09-23
        • 2018-10-02
        • 2023-03-23
        • 2020-04-07
        相关资源
        最近更新 更多