livewire / volt

Volt is an elegantly crafted functional API for Livewire.
https://livewire.laravel.com/docs/volt
MIT License
307 stars 19 forks source link

Volt::route() model binding not working for UUID for middleware() and can() ? #102

Closed pa2codes closed 2 months ago

pa2codes commented 2 months ago

Volt Version

1.0.0

Laravel Version

11.3

PHP Version

8.2

Database Driver & Version

MySQL 8 MacOS Sail

Description

I am trying to add Policy check to my Volt::route(), however Model Binding seems not to work for my Models using Uuid as primary key type for Volt route. Using standard Laravel Routing works as expected. Using authorize() in my Volt Component also works fine. $this->authorize('view', $product);

I tried middleware() as well as can() on Volt route.

Product migration

Schema::create('products', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');

Product Model

...
    public function getRouteKeyName(): string
    {
        return 'id';
    }

    protected $keyType = 'string';
...

ProductPolicy

...
    public function view(User $user, Product $product): bool
    {
        return $user->tenant->id == $product->tenant->id;
    }
...

routes/web.php

...
    Volt::route('/shop/product/{product}', 'pages.shop.single-product-page')
        ->name('single-product-page')
        >middleware('can:view,product');
...

Gate check returns 403

view [▼
  "ability" => "view"
  "result" => null
  "user" => "01bf0839-71ab-3c0b-8fbb-389448de83b5"
  "arguments" => "[0 => 20c5c5c5-3ad0-381d-9e1e-1b6f08ad9334]"
]

I am not sure if I am missing sth or of its a Volt bug. Appreciate your help! Thanks

Steps To Reproduce

1) Create Product Model with Uuid as primary key 2) Create ProductPolicy 3) Create Livewire Volt Component 4) Create Volt::route('/product/{product}', 'product-view') with middleware('can:view,product')

driesvints commented 2 months ago

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!