martin.fdm's avatar

Array to String Conversion in Listener trying to send Notification

Hi there! Got this exception while trying to send a Notification in a Listener.

As required for the notification I first get the notifiable users. Everything looks like this:

        $users = User::Role('supervisor')->get();
        debug($users);
        Notification::send($users, new NuevoFormularioExpedienteNotification($event->expediente));

Thing is it's not getting supervisor users (as shown by the debugger), while same line does in a controller. And there's the Array to String Conversion exception I'm not able to understand myself.

Here is the stack trace: https://flareapp.io/share/17DyjWZ5#F59

I'm a bit desperate. Every notification and job is now broken. This has been unexpected since it was working previously and the deadline is coming. Please help!

0 likes
23 replies
Snapey's avatar

you need to find out why user query is not returning any results

Sinnbeck's avatar

Why the uppercase R in Role?

        $users = User::role('supervisor')->get();
martin.fdm's avatar

Hi both of you! @sinnbeck @snapey

It's not even working with User::all() or $users = User::role('supervisor')->get()

And I should add that Debugbar shows: LOG.error: Array to string conversion {"userId":6,"exception":{}} which corresponds to the user that has triggered the use case, and therefore the event

martin.fdm's avatar

@Sinnbeck Thanks for the reply. I added the dd($e->getMessage()); in the try catch. It just returns "Array to string conversion". Added Xdebug and some breakpoints but, first time using it, I couldn't get it work properly I guess.

Sinnbeck's avatar

@martin.fdm Where did you add it?

It should look like

catch (Exception $e) {
         dd($e->getMessage());
            throw new QueryException(

                $query, $this->prepareBindings($bindings), $e

            );

        }
martin.fdm's avatar

@Sinnbeck I'm trying both on the service method which calls the event where I have all this situation and in the listener where $users = User::role('supervisor')->get() happens.. Anyhow, $query, $this->prepareBindings($bindings), is marked as undefined. Variables and method.

Edit : If I left it that way and send a request again, keep on displaying the Array to String Conversion Exception.

Sinnbeck's avatar

@martin.fdm Sorry what? Are you adding this inside your own files? My suggestion was to edit this file, just for testing

vendor/laravel/framework/src/Illuminate/Database/Connection.php line 704

Sinnbeck's avatar

@martin.fdm you shouldnt get error page, but rather a dd result page.. I dont assume that it is dd result you are getting?

martin.fdm's avatar

@Sinnbeck with that if statement it goes straight to Array to String Conversion Exception. Removing the ! returns this: ", " Hey, thanks again for your time! Really

martin.fdm's avatar

@Sinnbeck I should have added also that this seemed to be query which returns nothing.

select * from `users` where exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_id` and `model_has_roles`.`model_type` = 'App\Models\User' and `roles`.`id` in (2))

It's the equivalent query to $users = User::role('supervisor')->get()

martin.fdm's avatar

@cosmeoes @sinnbeck Hi ! I've just solved it! Main reason of this error, which doesn't seem to be answered in other posts across Internet, is because of NOT READING the documentation and aplying a workaround. I've had create a Notification model so I could access specific Notificaction instances like Notification::findOrFail($id)

Remove all that and notification and jobs are working.

Only thing is I don't seem to find the 'Eloquent way' to access a specific Notification without a Model, rather than with a DB query.

Thanks @sinnbeck for your help and time! I appreciate it !

Snapey's avatar

@martin.fdm Why do you need to access a specific notification? You have not mentioned this in your question or previous replies.

martin.fdm's avatar

@Snapey Just for the classic notification dropdown element (in my app, showing only unread notifications) where there's a message referencing issue of notification and a little button to redirect to pertinent page.

For that I've implement a handler which needs some way to get the specific notification in question.

Anyway! I've solved that requirement passing the whole notification instead of id for then searching it inside the handler! Hahah, my bad!

martin.fdm's avatar

@snapey mmmhm. I sang victory before time.

But I'll need to add a little bit more context .

In that dropdown element I've mentioned, I can use a foreach to handle notifications individually. lets say

foreach ($notifications as $notif)

But as there's no Notification model, when i pass $notif to handler, I'm only getting the notif id.

$notif is DatabaseNotification type

What should I do?

Please or to participate in this conversation.