rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.75k stars 330 forks source link

Passing $row in a request doesn't pass the object but the model class #563

Closed Mozzarella-dev closed 2 years ago

Mozzarella-dev commented 2 years ago

I've got a simple datatable with a delete button in each row, the cell of the button looks like this:

<x-livewire-tables::bs4.table.cell>
        <div>
        <form action="{{route('posts', $row)}}" method="POST">
            @csrf
            @method('DELETE')
        <button type="submit" class="badge badge-danger btn-xs">Delete</button>
        </form>
        </div>
</x-livewire-tables::bs4.table.cell>

This is my route:

Route::delete('/posts', [App\Http\Controllers\PostController::class, 'destroy'])->name('posts');

Finally the destroy method inside the controller looks like this:

public function destroy(Post $post)
    {
        dd($post);
    }

After clicking the button i get dumped the class of the Post model and not the post itself. (Looking into the attributes of the dump nothing shows up)

There's a way i can hotfix that?

rappasoft commented 2 years ago

Why are you not passing the post to the route by either ID or route model binding? It doesn't know how to find it in the route.

Like so:

Route::delete('/posts/{post}', [App\Http\Controllers\PostController::class, 'destroy'])->name('posts.destroy');
Mozzarella-dev commented 2 years ago

Following a tutorial i wasn't binding the model to the route, now it works properly, thanks for the head up!