Deleting rows with action button #1032
-
Hello everyone, first of all, I am very sorry for asking this question. I know many people have asked about action buttons before, and believe me when I say I looked at probably every single entry regarding this. But I found myself to be stuck nonetheless, which is why I am asking for help. As the title says, all I am trying to do is to add a button to my table that deletes the entry of the row. I already added a view and edit link, which works perfectly fine. Here are my files, if you need more information, please let me know!
<?php
namespace App\Http\Livewire;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ButtonGroupColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
use App\Models\Location;
class LocationTable extends DataTableComponent
{
protected $model = Location::class;
public function configure(): void
{
$this->setPrimaryKey('id');
$this->setPageName('locations');
$this->setPerPageVisibilityStatus(false);
}
public function columns(): array
{
return [
Column::make("ID", "id"),
Column::make("Name", "name")
->sortable()
->searchable(),
Column::make("Description", "description")
->collapseOnMobile(),
Column::make("Slug", "slug"),
ButtonGroupColumn::make('Actions')
->unclickable()
->attributes(function($row) {
return [
'class' => 'space-x-2',
];
})
->buttons([
LinkColumn::make('View')
->title(fn($row) => 'View')
->location(fn($row) => '/admin/locations/'.$row->id)
->attributes(function($row) {
return [
'class' => 'text-indigo-500',
];
}),
LinkColumn::make('Edit')
->title(fn($row) => 'Edit')
->location(fn($row) => '/admin/locations/'.$row->id.'/edit/')
->attributes(function($row) {
return [
'class' => 'text-indigo-500',
];
}),
]),
];
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Location;
use App\Http\Requests\LocationRequest;
class LocationsController extends Controller
{
public function index()
{
$locations= Location::all();
return view('admin.locations.index', ['locations'=>$locations]);
}
public function create()
{
return view('admin.locations.create');
}
public function store(LocationRequest $request)
{
$location = new Location;
$location->name = $request->input('name');
$location->description = $request->input('description');
$location->slug = $request->input('slug');
$location->save();
return redirect()->route('locations.index');
}
public function show($id)
{
$location = Location::findOrFail($id);
return view('admin.locations.show',['location'=>$location]);
}
public function edit($id)
{
$location = Location::findOrFail($id);
return view('admin.locations.edit',['location'=>$location]);
}
public function update(LocationRequest $request, $id)
{
$location = Location::findOrFail($id);
$location->name = $request->input('name');
$location->description = $request->input('description');
$location->slug = $request->input('slug');
$location->save();
return redirect()->route('locations.index');
}
public function destroy($id)
{
$location = Location::findOrFail($id);
$location->delete();
return redirect()->route('locations.index');
}
} My model only contains the relations. I used to try out quite many things that were also mentioned in this discussion forum and marked as solved, however, I am just not getting it to work :') I originally used this here in the index blade before changing over to laravel-livewire-tables, but I don't know how to incorporate it either: {!! Form::open(['method' => 'DELETE','route' => ['locations.destroy', $location->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!} Thank you so much in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I just added to the data table Class
and called through
|
Beta Was this translation helpful? Give feedback.
-
Thank you so much! I managed to get it to work, even if not exactly like you did. I ended up using a view, but your response helped me nonetheless! For anyone wondering in the future, I solved it like this:
<?php
namespace App\Http\Livewire;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ButtonGroupColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Location;
class LocationTable extends DataTableComponent
{
protected $model = Location::class;
public function configure(): void
{
$this->setPrimaryKey('id');
$this->setPageName('locations');
$this->setPerPageVisibilityStatus(false);
}
public function columns(): array
{
return [
Column::make("ID", "id"),
Column::make("Name", "name")
->sortable()
->searchable(),
Column::make("Description", "description")
->collapseOnTablet(),
Column::make("Slug", "slug"),
Column::make('Actions')
->label(
function ($row, Column $column) {
return view('admin.locations.actions')->withRow($row);
}
),
];
}
}
<div class="d-flex gap-2">
<a href="{{ route('locations.show', [$row->id]) }}" class="btn btn-info">Show</a>
<a href="{{ route('locations.edit', [$row->id]) }}" class="btn btn-primary">Edit</a>
{!! Form::open(['method' => 'DELETE','route' => ['locations.destroy', $row->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</div> It is obviously not good looking, yet, and also somewhat embarrassing that I didn't manage to do this before, but maybe it can help others either way :) |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm also trying to add a delete button but I couldn't and the examples they provide don't work for me
public function columns(): array
|
Beta Was this translation helpful? Give feedback.
I just added to the data table Class
and called through