Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rruben's avatar
Level 1

TALL stack update form problem.

Hello, I'm having problems with an update form with livewire/volt in Laravel 11 during validation:

public function submit(): void
{
    $this->form->name = strtoupper($this->form->name);
    $this->validate();
SQLSTATE[42S22]: Column not found: 1054 Unknown column '22' in 'where clause'
SELECT
  count(*) AS aggregate
FROM
  `platforms`
WHERE
  `22` = ACCUSANTIUM #19

the funny thing is 22 is the id of the record http://sgf.local/admin/show_platform/22

the route:

Volt::route('admin/show_platform/{platform}', 'admin.show_platform')
        ->name('admin.show_platform')
        ->can('admin', 'App\Models\User');

the volt component:

use Livewire\Volt\Component;
use Livewire\Attributes\Layout;

use App\Models\Country;
use App\Models\Platform;
use App\Livewire\Forms\EditPlatformForm;

new #[Layout('layouts.app')]
class extends Component {

    public EditPlatformForm $form;

    public function mount(Platform $platform): void
    {
        $this->form->platform   = $platform;
        $this->form->name       = $platform->name;
        $this->form->url        = $platform->url;
        $this->form->description= $platform->description;
        $this->form->image      = $platform->image;
        $this->form->status     = $platform->status;
        $this->form->country    = $platform->country;
        $this->form->repository = $platform->repository;
    }

    public function submit(): void
    {
        // Transformar el nombre a mayúsculas antes de la validación
        $this->form->name = strtoupper($this->form->name);

        $this->validate();

        $platform_updated = $this->form->update();
        l(__METHOD__,__LINE__,print_r($platform_updated,true));

        if( isset($platform_updated['error']) ){
            $error = isset($platform_updated['error']) ?
                        str_replace(['"', "'" ], '', $platform_updated['error'])
                        : '(?)';
            l(__METHOD__,__LINE__,"ERROR:" . $error);
            $this->js("swal_alert(
                        \"<b>".__('Error')."</b>\",
                        \"<i>".__('An error occurred while creating the platform')."</i><br><i>$error</i>\",
                        \"error\",
                        \"top-end\",
                        10000)");
            return;
        }

        $title = __('Platform was updated!');
        $description = __("...");
        $timer = 7000;

        $this->js("swal_alert('{$title}','{$description}','success','top-end',{$timer})");


    }

}; ?>

<div>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <h4 class="text-lg font-bold leading-tight text-gray-800">
                {{ __('Admin') }} 👉 {{ __('Show Platform') }} #{{ $this->form->platform->id }} "{{ $this->form->platform->name }}" {{ $this->form->platform->status_icon }} {{ App\Models\Country::COUNTRY_ICONS[$this->form->platform->country] }}
            </h4>
            <a href="#" class="px-4 py-2 text-sm font-semibold text-white bg-blue-500 rounded-md hover:bg-blue-600">
                {{ __('Statistics') }}
            </a>
        </div>
    </x-slot>

    <div class="px-20 py-2 my-0 bg-white">
        <form wire:submit="submit" class="space-y-5">
            <div class="grid grid-cols-1 gap-2 -mx-12 sm:grid-cols-2">
                <div>
                    <x-input
                        label="{{ __('Name') }}"
                        wire:model="form.name"
                    />
                </div>
                <div>
                    <x-input
                        label="{{ __('URL') }}"
                        wire:model="form.url"
                    />
                </div>
            </div>
            <div class="grid grid-cols-1 gap-2 -mx-12 sm:grid-cols-2">
                <div>
                    <div>
                        <x-input
                            label="{{ __('Description') }}"
                            wire:model="form.description"
                        />
                    </div>
                </div>
                <div>
                    <x-input
                        label="{{ __('Image') }}"
                        wire:model="form.image"
                    />
                </div>
            </div>
            <div class="grid grid-cols-1 gap-2 -mx-12 sm:grid-cols-2">
                <div>
                    <x-select
                        label="{{ __('Status') }}"
                        wire:model="form.status"
                        placeholder="{{ __('Select a status') }}"
                        value="{{ $this->form->status }}"
                    >
                        @foreach( Platform::STATUSES as $k => $status )
                            <x-select.option label="{{ __($status) }} {{ Platform::STATUS_ICONS[$k] }}" value="{{ $k }}"/>
                        @endforeach
                    </x-select>
                </div>
                <div>
                    <x-select
                        label="{{ __('Country') }}"
                        wire:model="form.country"
                        placeholder="{{ __('Select a country') }}"
                    >
                        @foreach( Country::COUNTRYS as $k => $country )
                            <x-select.option label="{{ __($country) }}" value="{{ $k }}" />
                        @endforeach
                    </x-select>
                </div>
            </div>
            <div class="grid grid-cols-1 gap-2 -mx-12 sm:grid-cols-1">
                <div>
                    <x-input
                        label="{{ __('Repository') }}"
                        wire:model="form.repository"
                    />
                </div>
            </div>
            <div class="grid grid-cols-1 gap-2 -mx-12 sm:grid-cols-2">
                <div class="flex justify-center mt-4 justify-left">
                    <x-button icon="arrow-left" secondary href="{{ route('admin.index') }}" class="mx-4" wire:navigate/>
                    <x-button type="submit" spinner="submit" primary label="{{ __('Edit platform') }}" />
                </div>
            </div>
        </form>
    </div>

</div>

screenshot of update form

the form component:

<?php

namespace App\Livewire\Forms;

use Livewire\Attributes\Validate;
use Livewire\Form;

use App\Models\Platform;
use App\Models\Country;
use App\Models\User;

use App\Mail\PlatformEdited;

use Faker\Factory as Faker;
use Str;
use DB;
use Mail;

use Exception;

class EditPlatformForm extends Form
{
    public Platform $platform;

    public string $name = "";
    public string $url = "";
    public string $description = "";
    public string $image = "";
    public int $status = 4;
    public int $country = 1;
    public string $repository = "";

    public function rules() // * estas reglas solo se disparan en el evento submit ( $this->validate(); )
    {
        return [
            'name'          => ['required','string','uppercase','unique:platforms,'.$this->platform->id,'min:3','max:50'],
            'url'           => ['required','string','active_url','unique:platforms,'.$this->platform->id,'max:255'],
            'description'   => ['nullable','string','max:255'],
            'image'         => ['nullable','string','active_url','unique:platforms,'.$this->platform->id,'max:255'],
            'status'        => ['required','integer','in:'.implode(',', array_keys(Platform::STATUSES))],
            'country'       => ['required','integer','in:'.implode(',', array_keys(Country::COUNTRYS))],
            'repository'    => ['nullable','string','active_url','unique:platforms,'.$this->platform->id,'max:255'],
        ];
    }

    /**
     * Attempt to authenticate/create the request's credentials and create the petition.
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(): array
    {
        l(__METHOD__,__LINE__, "update ...");
        $updated = [];

        try{
            DB::beginTransaction();

            $old_data = $this->platform->toArray();

            $this->platform->update([
                'name'          => $this->name,
                'url'           => $this->url,
                'description'   => $this->description,
                'image'         => $this->image,
                'status'        => $this->status,
                'country'       => $this->country,
                'repository'    => $this->repository,
                'user_id'       => auth()->id(),
            ]);

            $updated = [
                'name'          => $this->name,
                'url'           => $this->url,
                'description'   => $this->description,
                'image'         => $this->image,
                'status'        => $this->status,
                'country'       => $this->country,
                'repository'    => $this->repository,
            ];

            l(__METHOD__,__LINE__, "Platform was updated!:". print_r($updated, true));

            DB::commit();
        }catch(\Throwable $e){
            DB::rollback();

            l(__METHOD__,__LINE__, "❌ ERROR: {$e->getMessage()} @ LINE {$e->getLine()} IN {$e->getFile()}");
            l(__METHOD__,__LINE__, $e);

            $updated = [
                'error' => $e->getMessage()
            ];
        }
        return $updated;
    }
}
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

It will be one of your unique validation rules.

The syntax is unique:table,column,except,idColumn

You are passing platform->id as the column name

1 like
rruben's avatar
Level 1

@Snapey that was the problem, what a dummy 🙃

public function rules()
    {
        return [
            // #[Validate('required|string|uppercase|unique:platforms|min:3|max:50')]
            'name'          => ['required','string','uppercase','unique:platforms,name,'.$this->platform->id,'min:3','max:50'],
            // #[Validate('required|string|active_url|unique:platforms|max:255')]
            'url'           => ['required','string','active_url','unique:platforms,url,'.$this->platform->id,'max:255'],
            // #[Validate('nullable|string|max:255')]
            'description'   => ['nullable','string','max:255'],
            // #[Validate('required|string|active_url|unique:platforms|max:255')]
            'image'         => ['nullable','string','active_url','unique:platforms,image,'.$this->platform->id,'max:255'],
            // #[Validate('required|integer|in:1,2,3,4')]
            'status'        => ['required','integer','in:'.implode(',', array_keys(Platform::STATUSES))],
            // #[Validate('required|integer|in:1,2,3,4')]
            'country'       => ['required','integer','in:'.implode(',', array_keys(Country::COUNTRYS))],
            // #[Validate('nullable|string|active_url|unique:platforms|max:255')]
            'repository'    => ['nullable','string','active_url','unique:platforms,repository,'.$this->platform->id,'max:255'],
        ];
    }

Please or to participate in this conversation.