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

Time:11-04

I get a the below error when I try to update the modal text after a search performed

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

I have added a primary key name in the model that is different. See the model 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";
}

Here is the 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 file:

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

This only happens after the initial search. I think it is related to the Model some way but not sure how.

Any help would be great to solve this.

CodePudding user response:

I run your code, I only added to the TestSubItems the next properties

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

enter image description here

  • Related