laravel livewire - 更新 wire:modal 时列名无效 'id'

laravel livewire - Invalid column name 'id' on update of a wire:modal

执行搜索后尝试更新模式文本时出现以下错误

select * from [test] where [test].[id] in (?)
\vendor\laravel\framework\src\Illuminate\Database\Connection.php:703

我在模型中添加了一个不同的主键名称。见型号class:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
    use HasFactory;

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'testId';

    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    protected $connection = 'sqlsrv2';

    protected $table = "test";
}

这里是 livewire class:

<?php

namespace App\Http\Livewire;

use App\Models\TestSubItems;
use App\Models\Test;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class TestApp extends Component
{
    use AuthorizesRequests;

    public $search = '';
    public $items = null;
    public $subItems = null;
    public $message = '';
    public $warning = false;

    public function render()
    {
        return view('livewire.bin-app');
    }

    public function search()
    {
        $this->items = null;
        $this->subItems = null;

        if ($this->search) {
            $this->items = Test::where('ItemNo', $this->search)->get();
            if (!$this->items->isEmpty()) {
                $this->warning = false;
                $this->message = '';

                $this->subItems = TestSubItems::where('ItemNo', $this->search)->get();
            }

            if ($this->items->isEmpty()) {
                $this->message = 'Not found';
            }
        }
    }
}

Blade 文件:

<div>

    <div class="grid grid-cols-1 gap-4 p-4">
        <div>
            <label for="search" class="text-right">Item Code: </label>
            <input wire:model="search" autofocus="autofocus"
                   class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
                   id=" search"
            />
        </div>

        @if ($message != '')
            <div class="mb-5 mt-5 bg-red-50 rounded-md py-4 px-4">
                <p class="font-bold text-green-500">
                    {{ $message }}
                </p>
            </div>
        @endif

        <div>
            <button wire:click="search()" class="btn btn-primary">Search</button>
        </div>
    </div>

    @if (isset($items))
        @if (!$items->isEmpty())
            <div class="grid grid-cols-1 gap-4 p-4">
                @foreach($items as $item)
                    <h2 class="md:text-3xl text-xs"><span class="font-bold">Code:</span>
                            {{ $item->code }}
                    </h2>
                @endforeach
            </div>
        @endif
    @endif

    @if (isset($subItems))
        @if (!$subItems->isEmpty())

            <table class="table w-full">
                <thead>
                <th class="border-2 border-gray-50">
                    Item No
                </th>
                <th class="border-2 border-gray-50">
                    Type
                </th>
                </thead>
                <tbody>
                @foreach($subItems as $item)
                    <tr>
                        <td class="border-2 border-gray-50">
                            {{ $item->itemNo }}
                        </td>
                        <td class="border-2 border-gray-50">
                            {{ $item->type }}
                        </td>
                    </tr>
                @endforeach
            </table>
        @endif
    @endif

</div>

这只会在初始搜索之后发生。我认为它在某种程度上与模型相关,但不确定如何。

任何帮助都会很好地解决这个问题。

我运行你的代码,我只在TestSubItems下添加了属性

class TestSubItems extends Model
{
    use HasFactory;

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'itemNo';

    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';
}