laravel livewire exception: Queueing collections with multiple model connections is not supported

Viewed 1979

I´m beggining my Livewire journey and have created a list component which contains a form component for each of the list elements, and as I am trying to make it all work I keep getting the following never before seen exception:

LogicException Queueing collections with multiple model connections is not supported.

I use the same single connection for every model in my project, and I am not queueing anything on purpose in any of the components I'm currently working, so I have no clue as to where to start debugging this exception. The error message doesn´t help much either. In fact, I don´t even know what to post here for you guys to help me, short of posting the whole project... I guess I´m just angling for any clue as to where to start looking to get this fixed, so any help is much appreciated.

Here's the error message Stack trace:

C:\Users\bfcba\OneDrive\aplicaciones\duki\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Collection.php:705

public function getQueueableConnection()
{
    if ($this->isEmpty()) {
        return;
    }

    $connection = $this->first()->getConnectionName();

    $this->each(function ($model) use ($connection) {
        if ($model->getConnectionName() !== $connection) {
            throw new LogicException('Queueing collections with multiple model connections is not supported.');
        }
    });

    return $connection;
}

I´ll be happy to post all the information that you consider necessary. Just let me know.

Thanks in advance.

4 Answers

You can solve this by setup the connection in your model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ModelName extends Model
{
    protected $connection = "mysql";
}

This happens when you have a collection from the database, and you try to push a new object to that collection.

$this->members = $club->members;
$this->members->push(Member::make());

Someone from the Livewire forums suggested setting the connection of the new object to the same one used by collection objects, before pushing it to the said collection. However, this did not work for me.

$this->members = $club->members;
$newMember = Member::make()->setConnection('mysql')
$this->members->push($newMember);

A workaround that I found was to use an array instead of a collection.

$this->members = $club->members->all();
$newMember = Member::make();
$this->members[] = $newMember;

You can also convert it to a standard collection:

$this->members = new Collection($club->members);
$this->members->push(Member::make());

Set the connection name in the Model:

class ModelName extends Model
{
    protected $connection = "mysql";
}
Related