kiasaty's avatar

Database Notification Error on Lumen while sending notifications

Hi.

I'm trying to send notifications on Lumen when a new Offering (Model) is created. I added below packages and followed instructions on Laravel documentations:

    "illuminate/notifications": "^5.8", 
    "ramsey/uuid": "^3.8",

when I create a new model instance, the email is sent successfully, but Database Notification Channel has errors.

see my Notification Class:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class NewOffering extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * 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('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * 
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'id' => 1
        ];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

it gave me this error:

ErrorException: Array to string conversion in /vendor/illuminate/support/Str.php:353

the array returned from toDatabase() is supposed to convert to json automatically, but this doesn't happen. so I changed the following file as you see:

// vendor/illuminate/support/Str.php:353


    /**
     * Replace a given value in the string sequentially with an array.
     *
     * @param  string  $search
     * @param  array   $replace
     * @param  string  $subject
     * @return string
     */
    public static function replaceArray($search, array $replace, $subject)
    {
        $segments = explode($search, $subject);

        $result = array_shift($segments);
// dump($replace);
// dump($search, $replace, $subject, $segments, $result);
        foreach ($segments as $segment) {
            $temp = array_shift($replace) ?? 'null';
            $result .= json_encode($temp).$segment;
        }
dump($result);
        return $result;
    }

but another problem pops up:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' (SQL: insert into `notifications` (`id`, `type`, `data`, `read_at`, `user_id`, `updated_at`, `created_at`) values (\"d5ae9b3c-9114-43de-80b7-91de219fa835\", \"App\\Notifications\\NewOffering\", {\"id\":1}, \"null\", 1, \"2019-07-15 18:40:49\", \"2019-07-15 18:40:49\"))"

but there is no user_id in the notification table :/

I made the notification table using the following command:

php artisan notifications:table

table structure difference?!! :?

This is killing me,

any help would be appreciated...

0 likes
0 replies

Please or to participate in this conversation.