6

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.

1
  • 1
    Forgot to mention, this error happens -ocassionally- when refreshing the component after adding, updating or deleting a record. Commented Apr 3, 2021 at 1:52

5 Answers 5

7

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";
}
Sign up to request clarification or add additional context in comments.

Comments

6

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;

3 Comments

Thank you @howellmartinez. I was continously finding myself in dead end alleys using livewire, so I decided to abandon the technology and went back to Laravel - Vue, which is more familiar to me. Cheers!
FWIW, using array instead of pushing to the collection didn't work for me...
@E.Barney I'm pretty sure this is an Eloquent error not a Livewire error. Meaning if you did the same thing using a Vue front end you would get the same error. github.com/illuminate/database/blob/master/Eloquent/…
4

You can also convert it to a standard collection:

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

Comments

0

Set the connection name in the Model:

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

Comments

0

Just a little hint here when you cannot just set the connection into your model, in case you are using a multi-tenancy project for example.

When you 'make' a new model, it has it's $connection set to null. Our solution was to check for a null-value and then set the connection on the model according our config value.

public function getConnectionName()
{
    return $this->connection ?? config('database.default');
}

In our case, the config-value is set to the current Tenant using our product.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.