laravel livewire 异常:不支持具有多个模型连接的队列集合

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

我开始了我的 Livewire 之旅,并创建了一个列表组件,其中包含每个列表元素的表单组件,当我试图使其全部正常工作时,我不断收到以下前所未见的异常:

LogicException Queueing collections with multiple model connections is not supported.

我的项目中的每个模型都使用相同的单一连接,并且我没有故意在我当前工作的任何组件中排队,所以我不知道从哪里开始调试这个异常.错误消息也无济于事。事实上,除了 post 整个项目之外,我什至不知道要 post 在这里帮助你们什么……我想我只是在寻找任何线索从哪里着手解决这个问题,非常感谢您的帮助。

这是错误消息堆栈跟踪:

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;
}

我很乐意 post 提供您认为必要的所有信息。请告诉我。

提前致谢。

当您从数据库中获得一个集合并尝试将新对象推送到该集合时,就会发生这种情况。

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

Livewire 论坛中有人建议将新对象的连接设置为集合对象使用的相同连接,然后再将其推送到所述集合。但是,这对我不起作用。

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

我发现的解决方法是使用数组而不是集合。

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

您可以通过在模型中设置连接来解决此问题

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

您也可以将其转换为标准集合:

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

在模型中设置连接名称:

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