【问题标题】:laravel livewire - Invalid column name 'id' on update of a wire:modallaravel livewire - 更新电线时无效的列名“id”:模态
【发布时间】:2021-12-17 22:33:09
【问题描述】:

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

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

我在模型中添加了一个不同的主键名称。查看模型类:

<?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 类:

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

刀片文件:

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

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

任何帮助都可以解决这个问题。

【问题讨论】:

  • 这个问题来自组件的刀片吧?因为我没有看到任何在渲染上挂载或运行的调用。你能发布你的刀片视图吗?
  • 添加了@Prospero
  • 我不知道为什么不更新任何东西会产生错误。
  • 表名是否正确?受保护的$table =“测试”;它的表名不应该是“测试”???这是一个错字还是你有这样定义的?
  • 我确实删除了我的真实表名并将其替换为测试。该表没有主键。我确实尝试使用protected $primaryKey = null,但这并没有任何区别。

标签: php laravel laravel-8 laravel-livewire


【解决方案1】:

我运行你的代码,我只在 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';
}

【讨论】:

  • 太好了,我更新了错误的模型...但是为什么它会更新搜索?当您更改搜索文本时,该方法不应该执行,或者有更好的方法吗?
  • 在每次重新渲染时,livewire 都会对模型或属性进行再水化,在这种情况下,您已加载它然后抛出问题...我认为存在问题
猜你喜欢
  • 2023-01-30
  • 2022-08-17
  • 2021-02-20
  • 2022-07-14
  • 2021-08-29
  • 2021-09-14
  • 2021-01-13
  • 2022-10-17
  • 1970-01-01
相关资源
最近更新 更多