Workflow
Search With Relationship

This example uses Searching with Relationship.

ID
Dish
Category
Chef
Created At
1
Arkansas Possum Pie
Pie
03/11/2024
2
Albacore Tuna Melt
Soup
Vitor
01/12/2024
3
Π±ΠΎΡ€Ρ‰
Soup
Dan
02/12/2024
4
Bacalhau com natas
Garnish
22/11/2024
5
Baba Ghanoush
Soup
02/11/2024
6
Bacon Cheeseburger
Pasta
04/11/2024
7
Baked potato
Dessert
Dan
28/10/2024
8
Baklava
Pasta
06/12/2024
9
Bangers and mash
Soup
11/12/2024
10
Black Pudding
Meat
Claudio
18/12/2024
Showing 1 to 10 of 192 Results
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.
πŸ”Ž View on GitHub
<?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\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;

class SearchWithRelationshipTable extends PowerGridComponent
{
    public string $tableName = 'search-with-relationship-table';

    public int $categoryId = 0;

    public function setUp(): array
    {
        return [

            PowerGrid::header()
                ->showSearchInput(),

            PowerGrid::footer()
                ->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(),
        ];
    }
}