I am trying to create notifications in Laravel 5.8 with its own resources. For this I have created an authentication, with its migration and user model. There is nothing special here. It is the authentication that Laravel creates by default. I have created some users from tinker.
For the notifications table I have used php artisan notifications:table
, and I have created the table according to the migration generated by artisan. I have created the model like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
public $incrementing = false;
protected $keyType = 'string';
protected $table = 'notifications';
protected $fillable = [
'type',
'notifiable_type',
'notifiable_id',
'data',
'read_at',
];
}
Then I have created the notification class, with php artisan make:notification Messages
. In the class I have used the method toArray()
, like so:
<?php
namespace App\Notifications;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class Messages extends Notification
{
protected $mensaje;
use Queueable;
public function __construct($mensaje)
{
$this->mensaje = $mensaje;
}
public function via($notifiable)
{
return [
'database',
];
}
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
public function toArray($notifiable)
{
return $this->mensaje->toArray();
}
}
In the controller that receives the message from a form and must record the notification, I have the following:
public function sendMessage(Request $request)
{
$reglas = [
'mensaje' => 'required',
'destinatario' => 'required|exists:users,id',
];
$errores = [
'mensaje.required' => 'Debe incluir el texto del mensaje.',
'destinatario.required' => 'Debe seleccionar un destinatario.',
'destinatario.exists' => 'El destinatario elegido no existe.',
];
$request->validate($reglas, $errores);
$mensaje = Notification::create([
'remitente_id' => auth()->id(),
'destinatario_id' => $request->destinatario,
'mensaje' => $request->mensaje,
]);
$receptor = User::find($request->destinatario);
$receptor->notify(new Messages($mensaje));
return back()->with('grabado', 'El mensaje ha sido enviado');
}
Of course, I have made sure that the names of the variables received in the request are the same as those of the form fields. There is no mistake there.
The case is that, when trying to execute it, it throws me an exception like the following:
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `notifications` (`updated_at`, `created_at`) values (2020-05-10 11:33:25, 2020-05-10 11:33:25))
I figured that since the notification table has created the field id
as a uuid
primary type (this is done by artisan by default) instead of the typical autoincrementable, I should set a value for this field, so I modified the object $mensaje
in the controller, like this:
$mensaje = Notification::create([
'id' => uniqid(),
'remitente_id' => auth()->id(),
'destinatario_id' => $request->destinatario,
'mensaje' => $request->mensaje,
]);
To do this, in the model Notification
I added the field id
to the array $fillable
. The exception then changes to the following:
SQLSTATE[HY000]: General error: 1364 Field 'type' doesn't have a default value (SQL: insert into `notifications` (`id`, `updated_at`, `created_at`) values (5eb7ed23847b6, 2020-05-10 12:01:39, 2020-05-10 12:01:39))
So, now it asks me for a value for type
. I add it in the controller, like so:
$mensaje = Notification::create([
'id' => uniqid(),
'type' => 'Mensaje',
'remitente_id' => auth()->id(),
'destinatario_id' => $request->destinatario,
'mensaje' => $request->mensaje,
]);
And then the exception changes to:
SQLSTATE[HY000]: General error: 1364 Field 'notifiable_type' doesn't have a default value (SQL: insert into `notifications` (`id`, `type`, `updated_at`, `created_at`) values (5eb7edb689245, Mensaje, 2020-05-10 12:04:06, 2020-05-10 12:04:06))
And here things get worse. notifiable_id
The y fields are supposed to notifiable_type
take them directly, right? Anyway, I added them in the controller, like this:
$mensaje = Notification::create([
'id' => uniqid(),
'type' => 'Mensaje',
'notifiable_id' => $request->destinatario,
'notifiable_type' => User::class,
'remitente_id' => auth()->id(),
'destinatario_id' => $request->destinatario,
'mensaje' => $request->mensaje,
]);
And now the exception is the following:
SQLSTATE[HY000]: General error: 1364 Field 'data' doesn't have a default value (SQL: insert into `notifications` (`id`, `type`, `notifiable_id`, `notifiable_type`, `updated_at`, `created_at`) values (5eb7ee751d6a1, Mensaje, 4, App\User, 2020-05-10 12:07:17, 2020-05-10 12:07:17))
All of this brings me to two basic questions: How do notifications work? and, the most important. If I have to define everything manually in the controller, what are the notification classes for? How is the correct way to do this to take advantage of these resources? I have been looking at the official documentation, and a lot of sites on the Internet, but I can't find any working example that allows me to deduce the correct way to use it.
An example log:
[2020-05-14 15:49:25] local.ERROR: Array to string conversion {"userId":1,"exception":"[object] (ErrorException(code: 0): Array to string conversion at C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Str.php:353)
[stacktrace]
#0 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Str.php(353): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Array to string...', 'C:\\\\laragon\\\\www\\\\...', 353, Array)
#1 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\QueryException.php(56): Illuminate\\Support\\Str::replaceArray('?', Array, 'insert into `no...')
#2 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\QueryException.php(39): Illuminate\\Database\\QueryException->formatMessage('insert into `no...', Array, Object(ErrorException))
#3 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(665): Illuminate\\Database\\QueryException->__construct('insert into `no...', Array, Object(ErrorException))
#4 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(624): Illuminate\\Database\\Connection->runQueryCallback('insert into `no...', Array, Object(Closure))
#5 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(459): Illuminate\\Database\\Connection->run('insert into `no...', Array, Object(Closure))
#6 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php(411): Illuminate\\Database\\Connection->statement('insert into `no...', Array)
#7 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php(2646): Illuminate\\Database\\Connection->insert('insert into `no...', Array)
#8 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Builder.php(1353): Illuminate\\Database\\Query\\Builder->insert(Array)
#9 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(815): Illuminate\\Database\\Eloquent\\Builder->__call('insert', Array)
#10 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(667): Illuminate\\Database\\Eloquent\\Model->performInsert(Object(Illuminate\\Database\\Eloquent\\Builder))
#11 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany.php(281): Illuminate\\Database\\Eloquent\\Model->save()
#12 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php(1124): Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany->Illuminate\\Database\\Eloquent\\Relations\\{closure}(Object(App\\Notification))
#13 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany.php(282): tap(Object(App\\Notification), Object(Closure))
#14 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\Channels\\DatabaseChannel.php(20): Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany->create(Array)
#15 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\NotificationSender.php(144): Illuminate\\Notifications\\Channels\\DatabaseChannel->send(Object(App\\User), Object(App\\Notifications\\UsersMessages))
#16 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\NotificationSender.php(103): Illuminate\\Notifications\\NotificationSender->sendToNotifiable(Object(App\\User), '4cf6a610-65b7-4...', Object(App\\Notifications\\UsersMessages), 'database')
#17 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\Localizable.php(19): Illuminate\\Notifications\\NotificationSender->Illuminate\\Notifications\\{closure}()
#18 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\NotificationSender.php(105): Illuminate\\Notifications\\NotificationSender->withLocale(NULL, Object(Closure))
#19 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\NotificationSender.php(77): Illuminate\\Notifications\\NotificationSender->sendNow(Object(Illuminate\\Database\\Eloquent\\Collection), Object(App\\Notifications\\UsersMessages))
#20 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\ChannelManager.php(39): Illuminate\\Notifications\\NotificationSender->send(Object(Illuminate\\Database\\Eloquent\\Collection), Object(App\\Notifications\\UsersMessages))
#21 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Notifications\\RoutesNotifications.php(18): Illuminate\\Notifications\\ChannelManager->send(Object(App\\User), Object(App\\Notifications\\UsersMessages))
#22 C:\\laragon\\www\
otificaciones\\app\\Http\\Controllers\\HomeController.php(75): App\\User->notify(Object(App\\Notifications\\UsersMessages))
#23 [internal function]: App\\Http\\Controllers\\HomeController->sendMessage(Object(Illuminate\\Http\\Request))
#24 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): call_user_func_array(Array, Array)
#25 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('sendMessage', Array)
#26 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(219): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\HomeController), 'sendMessage')
#27 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(176): Illuminate\\Routing\\Route->runController()
#28 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(680): Illuminate\\Routing\\Route->run()
#29 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#30 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#31 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#32 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#33 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php(43): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#34 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#35 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#36 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(75): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#37 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#38 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#39 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#40 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#41 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#42 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(56): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#43 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#44 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#45 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#46 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#47 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#48 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(66): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#49 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#50 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#51 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#52 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(682): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#53 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#54 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(623): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#55 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#56 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#57 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#58 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#59 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#60 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#61 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#62 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#63 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#64 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#65 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#66 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#67 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#68 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#69 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#70 C:\\laragon\\www\
otificaciones\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#71 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(163): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#72 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#73 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#74 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#75 C:\\laragon\\www\
otificaciones\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#76 C:\\laragon\\www\
otificaciones\\public\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#77 {main}
"}
These would be the basic steps to send a notification by email, save the notifications sent in the Database, and pass data from the controller to the notification to save them in the DB and customize the message.
Send by Email
Assuming you already have the credentials set up to send emails and that's working...
You create the notification class:
You make sure that in your model you
User
use the traitNotifiable
(it already comes by default, but just in case):And in the controller you can do something like this:
So far, with just that, the notification would be sent by email when accessing that method of the controller.
Save to Database
To save the notifications in the Database, create the migration and run it:
Add the element
'database'
to the array returned by the methodvia()
of your notification class:And in the method
toArray()
, return an array with data that you want to be saved:Ready, the notifications will be saved in the database.
Customize the message and what is saved in the DB
In the controller build an array and pass it to your notification class as an argument:
And in the notification class receive the argument in the constructor:
That's how I made it work.
The thing about creating a model and inserting the notification manually as samples in the question does not work.
The problem comes because, although
$mensaje
in the controller it is an object of the class that you created (in your example,Notification
), it arrives in the notification class as an array.The solution is to change the class controller, like so:
With this, you are already handling a collection, which is what you need in notifications.