Workflow
Actions From View

This example uses the Actions From View to display buttons loaded from a Blade Component.

Dish Name
Category
Price
In Stock
Created At
Action
Arkansas Possum Pie
Soup
118,08 €
No
26/09/2024
- out of sock -
Albacore Tuna Melt
Pasta
132,75 €
No
07/10/2024
- out of sock -
борщ
Soup
100,19 €
Yes
26/09/2024
Bacalhau com natas
Dessert
208,60 €
No
29/09/2024
- out of sock -
Baba Ghanoush
Soup
183,81 €
No
03/10/2024
- out of sock -
Bacon Cheeseburger
Soup
137,48 €
No
11/09/2024
- out of sock -
Baked potato
Pasta
189,66 €
Yes
03/10/2024
Baklava
Pie
161,92 €
Yes
12/10/2024
Bangers and mash
Pie
214,04 €
Yes
03/10/2024
Black Pudding
Pasta
116,13 €
Yes
03/10/2024
Disclaimer: Table data is randomly generated for illustrative purposes only. The information here is not a reflection of the actual market and does not constitute business, financial, or medical advice.
<?php
 
namespace App\Livewire\Examples\ActionsFromViewTable;
 
use App\Models\Dish;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Number;
use Illuminate\View\View;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class ActionsFromViewTable extends PowerGridComponent
{
public string $tableName = 'actions-from-view-table';
 
public function setUp(): array
{
return [
PowerGrid::header()
->showSearchInput(),
 
PowerGrid::footer()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): ?Builder
{
return Dish::query()
->join('categories as newCategories', function ($categories) {
$categories->on('dishes.category_id', '=', 'newCategories.id');
})
->select('dishes.*', 'newCategories.name as category_name')
->toBase();
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('price')
->add('category_id', fn ($dish) => intval($dish->category_id))
->add('category_name', fn ($dish) => e($dish->category_name))
->add('price_in_eur', fn ($dish) => Number::currency($dish->price, in: 'EUR', locale: 'pt_PT'))
->add('in_stock', fn ($dish) => $dish->in_stock ? 'Yes' : 'No')
->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y'));
}
 
public function columns(): array
{
return [
Column::make('Dish Name', 'name')
->bodyAttribute('!text-wrap')
->searchable()
->sortable(),
 
Column::make('Category', 'category_name'),
 
Column::make('Price', 'price_in_eur', 'price'),
 
Column::make('In Stock', 'in_stock'),
 
Column::make('Created At', 'created_at_formatted'),
 
Column::action('Action'),
];
}
 
public function actionsFromView($row): View
{
return view('actions-view', ['row' => $row]);
}
}
Code highlighting provided by Torchlight.dev