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

Mohamed Wannous picture Mohamed Wannous · Apr 30, 2017 · Viewed 16.6k times · Source

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

In my AddPost.php file i wrote this code:

<?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()

How can i fix this one please??
Thanks.

Answer

Marcin Nabiałek picture Marcin Nabiałek · Apr 30, 2017

Instead of:

use Illuminate\Notifications\Notification;

you should use

use Notification;

Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.