Laravel notifications does not store column data - laravel

Notifications are not recording data in the database. I tried it with toDatabase and it also didn't work. Look:
<?php
namespace App\Notifications;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NovoSeguidor extends Notification implements ShouldQueue
{
use Queueable;
private $user;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(user $user)
{
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail','database'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line("Novo seguidor: {$this->user}")
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'teste' => 'Hello',
];
}
}
My column data return [].
I tried both ways, both using toArray() and toDatabase(), both return an empty array.
I've tried everything and I don't see anything that could be causing it. Can anyone help me identify the problem?

Related

Laravel Echo.js not receiving notification from pusher no log message

I followed up some tutorials about echo and pusher. I configured all the project as necessary but object notifications not appear at browser. I uncoment the line
<?php
namespace App\Notifications;
use App\Models\Admin;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class RegisterNewNotification extends Notification implements ShouldBroadcast
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public $message;
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['broadcast'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toDatabase($notifiable)
{
return [
'message' => $this->message,
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'message' => $this->message,
]);
}
public function toArray($notifiable)
{
return [
//
];
}
}
Broadcast::channel('App.Models.Admin.{adminId}', function ($admin, $adminId) {
return $admin->id === $adminId;
});
Route::get('noti',function(){
$user = Admin::first();
$user->notify(new RegisterNewNotification("Hello"));
});
<script type="module">
Echo.private('App.Models.Admin.1')
.notification((notification) => {
console.log(notification.message); // no log
});
//Echo.channel('events').listen('NewUserRegister', (e) => console.log("RealTimeEventMessage: "+e.message));
</script>
enter image description here
I followed up some tutorials about echo and pusher. I configured all the project as necessary but object notifications not appear at browser. I uncoment the line

I have multiple Notifications dispatched at once but only one is sent and no error is logged

Notification::sendNow(Admin::all(),new NewMessageNotification($tic,$message,Admin::first()));
Notification::sendNow(Admin::all(),new TicketCreatedNotification($tic));
my first notification class
class NewMessageNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(private Ticket $ticket,private TicketChat $chat,private $user)
{
//
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database','broadcast'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable): array
{
return $this->chat->append(['from','to'])->toArray()+['ticket'=>$this->ticket->toArray()];
}
public function toBroadcast($notifiable): array
{
$count = ['un_read_side_bar_count'=>$this->user->myUnReadSideBar()->count()];
if($this->user instanceof Admin){
$count+=['unread_sellers_chat'=>$this->user->myUnreadChatsSellers()->count(),'unread_users_chat'=>$this->user->myUnreadChatsUsers()->count()];
}
return $this->chat->append(['from','to'])->toArray()+['ticket'=>$this->ticket->toArray()]+$count;
}
public function broadCastType(){
return 'broadcast.message.received';
}
}
my second notification class
class TicketCreatedNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(public Ticket $ticket)
{
//
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database','broadcast'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'ticketable'=>[
'name'=>$this->ticket->ticketable->name,
],
'subject'=>$this->ticket->subject,
'identifier'=>$this->ticket->identifier,
'un_read_user_messages_count'=>$this->ticket->un_read_user_messages_count,
'un_read_admin_messages_count'=>$this->ticket->un_read_admin_messages_count,
'created_at'=>Carbon::parse($this->ticket->created_at)->format('Y-m-d H:i:s'),
];
}
public function broadCastType(){
return 'broadcast.ticket.created';
}
}
enter image description here
from image of telescope above it shows pending but in jobs table and failed_jobs its not there, I have tried delaying the job but still to no avail I removed the implement ShouldQueue still no result is there anything I am missing

how to fix the correct url in the button of email laravel notification?

i'm using laravel mail notification and i have problem in the url button i have http://127.0.0.1 instead of http://127.0.0.1:8000 and when i clic at the button in email i redirected in failued url because :8000
is missed.
how i can fix this problem please ?
this my code in notification class
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
class NewProject extends Notification implements ShouldQueue
{
use Queueable;
public $data;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($data)
{
$this->data=$data;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('New Project :' . $this->data['projet'])
->action('New project', url('/detail/'.$this->data['id']))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toDatabase($notifiable)
{
return [
//
];
}
}```
The base app URL is configured inside the .env file on the variable APP_URL. So you must set:
APP_URL=http://127.0.0.1:8000
In your environment file.

Why is the data column empty? Laravel Notifications

I am trying to set up database notification, but the data I am sending for the 'data' column isn't showing up...
My notification class:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\User;
use App\Admin;
class UserAddedToStudio extends Notification implements ShouldQueue
{
use Queueable;
public $user;
public $admin;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(User $user, Admin $admin)
{
$this->user = $user;
$this->admin = $admin;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Welcome to '.$this->admin->name.'\'s studio')
->line('Hi '.$this->user->name.'. Your request to join '.$this->admin->name.'\'s studio has been accepted! Now you can start registering for lessons.')
// ->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toDatabase($notifiable)
{
return [
'message' => 'You have been added to the studio'
];
}
}
In my notifications table on MySQL all the columns are filled except for the data column. There I only get an empty array []. but why?? so frustrating.... :(
Here is the screenshot from my phpmyadmin.
Try to use toArray() instead of toDatabase()
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'message' => 'You have been added to the studio'
];
}
as you use Queue you should restart the queue worker.
php artisan queue: work
Remember, queue workers, are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers. In addition, remember that any static state created or modified by your application will not be automatically reset between jobs.

Laravel 5.7 Queue function but not send email

i have created a notification with ShouldQueue and my "job" table is populated but, when i launch a command: "php artisan queue:listen" the rows in table are processed but not send the email.
If don't use the queue all code function and email are send to destination.
I use the markdown for send email.
Command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
//
use Notification;
use App\Notifications\Listini\NotifyListinoUpdate;
class StoreListinoOil extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:storelistinooil';
/**
* The console command description.
*
* #var string
*/
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$details = array(
'day_listino' => 'today',
'email' => 'email#email.com'
);
Notification::send($details, new NotifyListinoUpdate($details));
dd('OK');
}
}
Notification -> NotifyListinoUpdate
namespace App\Notifications\Listini;
// use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
// use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
//
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class NotifyListinoUpdate extends Notification implements ShouldQueue
{
// use Queueable;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $details;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Notifica - '.$this->details['day_listino'])
->markdown('mail.admin.listino_update',['details'=> $this->details]);
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
You have to run the Queue worker using following command:
php artisan queue:work
Check the documentation on:
Running Queue Workers

Resources