4

I have a Controller which listens to a new Schedule creation and sends the result back to the view via ajax. Inside of it I want to add a Notification to send email to the user once the Schedule cannot be completed due to a lack of resources at that specific date and time.

The problem is that I get the error below:

Class 'App\Http\Controllers\Notification' not found in /laravel/app/Http/Controllers/DadosAgendamentoController.php on line 89

The folder structure is this:

-app
    -Http
        -Controllers 
            DadosAgendamentoController.php
    -Notifications
        AgendamentoPendente.php

DadosAgendamentoController.php head code:

namespace App\Http\Controllers;

use Input;
use Request;
use App\Servicos;
use App\Disponibilidades;
use App\Estabelecimentos;
use App\HorariosEstabelecimento;
use App\Agendamento;
use App\User;
use App\Notifications\AgendamentoPendente;

line 88 and 89:

$user = User::where('id',1)->get();
Notification::send($user, new AgendamentoPendente(1));

Trough my Controller I can access all the classes above, but not the AgendamentoPendente

My goal is to send an email do the admin so he can suggest a new date and time to the client when the resources are not available at the desired date and time.

How can it be fixed? Can I access the class in this Controller? How?

Jaqueline Passos
  • 1,301
  • 2
  • 24
  • 37
  • Please show us `89th line of DadosAgendamentoController.php` – Alexey Mezenin Jan 23 '17 at 17:22
  • I almost sure you haven't imported `Notification` class. Just import the namespace and it should be fixed. If Notification is a facade, just invoke by `\Notification::foo()` – felipsmartins Jan 23 '17 at 17:25
  • @AlexeyMezenin, I've edited the question. – Jaqueline Passos Jan 23 '17 at 17:26
  • @felipsmartins, I've seen that User has a default config for using the Notifications and I've tried to use also the command `use Illuminate\Notifications\Notifiable;`, no success, though – Jaqueline Passos Jan 23 '17 at 17:27
  • @JaquelinePassos The Alexey Mezenin's answer is right. Even though it is not solve your question, theres a issue in another place/scope. – felipsmartins Jan 23 '17 at 17:40
  • @felipsmartins is that because Notifications is linked with the database and it is only triggered once the data is saved? Is that how Notifications works in Laravel? Do you know any better approach to my case? I've thought about creating a boolean status field in my database, can I trigger a Notification if the status is set to false. I still get an error, and the email are not being sent... Searching the web I guess the reason is that the notifications are only sent if a new data of the model is saved on my database, am I right? – Jaqueline Passos Jan 24 '17 at 22:28

6 Answers6

10

Notifications may be sent in two ways: using the notify method of the Notifiable trait or using the Notification facade.

https://laravel.com/docs/5.3/notifications#sending-notifications

Option 1

You can use notify() method:

$user->notify(new AgendamentoPendente(1));

Also, make sure User class uses Notifiable trait:

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

Option 2

Using facade with full namespace:

\Notification::send($user, new AgendamentoPendente(1));
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • The error is solved but It didn't worked as expected... Laravel Notifications are sent only if a new data of a model is created on the database? Is this how it works? Can I send it only if a table field value is set to false? – Jaqueline Passos Jan 24 '17 at 22:45
5

Add use Notification; in your controller

OR

alternatively, use \Notification::send($user, new AgendamentoPendente(1));

Paras
  • 9,258
  • 31
  • 55
0

add this at the top of the controller:

use App\Notifications\AgendamentoPendente;

i was having the same problem and this fixed it

0

Also note that if you are using the facade, make sure your User queries the email field from your database

$users = User::select("email")->get();
\Notification::send($users, new AgendamentoPendente(1));
Kwaye Kant
  • 47
  • 1
  • 6
0

You have to use facades at the top

use Illuminate\Support\Facades\Notification;

You can refer to this tutorial

https://thecodingsolution.com/view/laravel-notofication-laravel-database-notification

0

You can pull in the notification library used Lumen 8.0:

"illuminate/notifications": "5.3.*" into your composer.json then running composer update to pull in the notification libraries.

You will also need to add

$app->register(Illuminate\Notifications NotificationServiceProvider::class);

to your bootstrap/app.php

This process working for me. Thanks

Saif uddin
  • 459
  • 4
  • 7