Workflow
Search With Relationship

This example uses Searching with Relationship.

ID
Dish
Category
Chef
Created At
1
Arkansas Possum Pie
Garnish
06/07/2024
2
Albacore Tuna Melt
Pie
Vitor
03/07/2024
3
борщ
Soup
Dan
26/06/2024
4
Bacalhau com natas
Garnish
Dan
02/06/2024
5
Baba Ghanoush
Pasta
Vitor
30/05/2024
6
Bacon Cheeseburger
Fish
13/06/2024
7
Baked potato
Garnish
05/07/2024
8
Baklava
Dessert
14/07/2024
9
Bangers and mash
Dessert
Luan
26/05/2024
10
Black Pudding
Pasta
Dan
10/07/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\SearchWithRelationshipTable;
 
use App\Models\Dish;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class SearchWithRelationshipTable extends PowerGridComponent
{
public int $categoryId = 0;
 
public function setUp(): array
{
return [
 
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): Builder
{
return Dish::query()
->when(
$this->categoryId,
fn ($builder) => $builder->whereHas(
'category',
fn ($builder) => $builder->where('category_id', $this->categoryId)
)
->with(['category', 'kitchen'])
);
}
 
public function relationSearch(): array
{
return [
'category' => [
'name',
],
 
'chef' => [
'name',
],
];
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('chef_name', fn ($dish) => e($dish->chef?->name))
->add('category_name', fn ($dish) => e($dish->category->name))
->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y'));
}
 
public function columns(): array
{
return [
Column::make('ID', 'id')
->searchable()
->sortable(),
 
Column::make('Dish', 'name')
->searchable()
->sortable(),
 
Column::make('Category', 'category_name')
->searchable(),
 
Column::make('Chef', 'chef_name')
->searchable(),
 
Column::make('Created At', 'created_at_formatted', 'created_at')
->sortable(),
];
}
}
Code highlighting provided by Torchlight.dev

Here you can find all relevant packages installed on this demo.

Name
Version
Description
laravel/framework
v11
The Laravel Framework.
livewire/livewire
v3.5.4
A front-end framework for Laravel.
openspout/openspout
v4.24.3
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
5.x-dev
PowerGrid generates Advanced Datatables using Laravel Livewire.