martin.fdm's avatar

Array to String Conversion Error Exception on Notification::send

Hi everyone!

Suddenly I'm getting " Error Exception Array to String Conversion " when trying to send a notification. It's so unexpected because it has worked previously and haven't changed the file. I've been running some 'migration fresh', but still.. can't understand the issue.

$expediente = Expediente::find(2);

User::Role('supervisor')
->each(function(User $user) use($expediente){
    Notification::send($user, new NuevoFormularioExpedienteNotification($expediente));
});

Even try with User::Role('supervisor')->get() but doesn't make a difference.

Any help would be appreciated !

0 likes
10 replies
tykus's avatar

The error is on which line - it is not apparent from the snippet you shared.

Alos, you know you can pass the Collection of User instances to the send method?

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

https://laravel.com/docs/8.x/notifications#using-the-notification-facade

martin.fdm's avatar

@tykus Hi!

Error is on Notification::send line;

Thing is I'm sending this with event and listeners, so its actually

$users = User::Role('supervisor')->get();

Notification::send($users, new NuevoFormularioExpedienteNotification($event->expediente));

But even so, same error

martin.fdm's avatar

@tykus

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError vendor/laravel/framework/src/Illuminate/Support/Str.php:596 .

public static function replaceArray($search, array $replace, $subject){

	$segments = explode($search, $subject);
	$result = array_shift($segments);
	foreach ($segments as $segment) {
		$result .= (array_shift($replace) ?? $search).$segment;    // error line
	}	

	return $result;

} 
tykus's avatar

@martin.fdm no clearer... this is why I asked about a stack trace... what is calling replaceArray?

Formatting... wrap code in ```

martin.fdm's avatar

@tykus Hi again! Since you've been the only one to response, I ask you please, when you have a time, to check the stack trace. I see the problem is related to the database. The thing is I neither understand why "replaceArray()" it's been called. Let me make clear that User:Role('supervisor') is retrieving two user models correctly. This was working properly.

Thanks in advance!

SilenceBringer's avatar

@martin.fdm Maybe you look to the wrong part at all? Maybe this part

new NuevoFormularioExpedienteNotification($expediente)

throws an error? maybe your NuevoFormularioExpedienteNotification expects string as the param, but receive array?

Please or to participate in this conversation.