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

igor1523's avatar

URL attribute not working Livewire 3 Laravel 12

Livewire URL attribute is not updating the URL.

I'm using Laravel 12 starter kit and even I put the URL param directly in the link, it also don't work. When I change in selects, nothing happen.

There's no error in console and requests returns 200.

#[Url(as: 'tabela', history: true)] public $table = '';

example.com?tabela=products

don't work, if I put a default table in the public $table, it will be there forever, even If I change the link or the select dropdown.

The only thing that didn't react is the URL, all the other component react.

0 likes
1 reply
igor1523's avatar

namespace App\Livewire;

use App\Models\Product; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Livewire\Attributes\On; use Livewire\Attributes\URL; use Livewire\Component; use Livewire\WithPagination;

class Crud extends Component { use WithPagination;

#[Url(as: 'tabela', history: true)]
public $table = 'products';

public $tables = [];

public $columns = [];

public $columnTypes = [];

public $editingId = null;

public $inputs = [];

public function mount()
{
    $this->tables = $this->getAllTables();
    $this->getColumnsAndTypes();
    $this->resetInputs();
}

public function getAllTables()
{
    return [
        'products',
        'users',
        'equipments',
        'configs',
        'received_requests',
    ];
}

public function getColumnsAndTypes()
{
    $columns = array_values(array_filter(
        Schema::getColumnListing($this->table),
        fn ($column) => ! in_array($column, ['id', 'updated_at', 'deleted_at', 'products'])
    ));

    $types = [];
    foreach ($columns as $column) {
        $types[$column] = Schema::getColumnType($this->table, $column);
    }
    $this->columns = $columns;
    $this->columnTypes = $types;
}

public function edit($id)
{
    $record = DB::table($this->table)->where('id', $id)->first();
    foreach ($this->columns as $column) {
        $this->inputs[$column] = $record->$column ?? '';
    }
    $this->editingId = $id;
}

public function delete($id)
{
    DB::table($this->table)->where('id', $id)->delete();
    $this->resetInputs();
    $this->editingId = null;
}

public function resetInputs()
{
    foreach ($this->columns as $column) {
        $this->inputs[$column] = '';
    }
}

public function resetForm()
{
    $this->resetInputs();
    $this->editingId = null;
}

public function updateTable()
{
    $this->getColumnsAndTypes();
    $this->resetInputs();
}

#[On('print-stock-label')]
public function printStockLabel($productId, $copies = 1)
{
    $product = Product::find($productId);
    if ($product->printStockLabel($copies)) {
        $this->modal("print-stock-label$productId")->close();
        session()->flash('success', "$copies da etiqueta do produto $product->name foram enviadas para impressão.");
        $this->dispatch('render-message');
    }
}

#[On('update-stock')]
public function updateStock($productId, $quantity = 1)
{
    $product = Product::find($productId);
    if ($product->updateStock($quantity)) {
        $this->modal("update-stock$productId")->close();
        session()->flash('success', "$quantity do produto $product->name foram atualizados no estoque.");
        $this->dispatch('render-message');

    }

}

#[On('form-reseted')]
public function formReseted()
{
    foreach ($this->inputs as $column => $input) {
        $this->inputs[$column] = null;
    }
    $this->editingId = null;
    $this->render();
}

#[On('record-saved')]
public function render()
{
    $records = DB::table($this->table)->orderByDesc('id')->paginate(20);

    return view('livewire.crud', [
        'records' => $records,
        'columnTypes' => $this->columnTypes,
    ]);
}

}

Please or to participate in this conversation.