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

3

12

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.

Errand answered 30/4, 2017 at 12:14 Comment(0)
B
43

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.

Boxer answered 30/4, 2017 at 12:21 Comment(0)
S
11

Using this use Illuminate\Support\Facades\Notification;

instead of this use Illuminate\Notifications\Notification;

solved the problem for me.

Hope this helps someone.

Search answered 9/8, 2020 at 21:18 Comment(1)
Thank you! Using the Facades path resolved a similar issue for me.Pelvis
A
0

using this is better

use Notification

Instead of

use Illuminate\Support\Facades\Notification

this makes the send() not accessible [#Notification Databse]

Adkisson answered 31/7, 2021 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.