Datasource Join
This example demonstrates the use of a Join Relationship in a Datasource.
ID
|
Dish
|
Category
|
---|---|---|
1
|
Arkansas Possum Pie
|
Pie
|
2
|
Albacore Tuna Melt
|
Soup
|
3
|
Π±ΠΎΡΡ
|
Soup
|
4
|
Bacalhau com natas
|
Garnish
|
5
|
Baba Ghanoush
|
Soup
|
6
|
Bacon Cheeseburger
|
Pasta
|
7
|
Baked potato
|
Dessert
|
8
|
Baklava
|
Pasta
|
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\DatasourceJoinTable;
use App\Models\Dish;
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
class DatasourceJoinTable extends PowerGridComponent
{
public string $tableName = 'datasource-join-table';
public ?string $primaryKeyAlias = 'id';
public string $primaryKey = 'dishes.id';
public function setUp(): array
{
return [
PowerGrid::header()
->showSearchInput(),
PowerGrid::footer()
->showPerPage(8, [8, 15, 25]),
];
}
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');
}
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('category')
->add('name')
->add('category_name');
}
public function columns(): array
{
return [
Column::make('ID', 'id')
->searchable()
->sortable(),
Column::make('Dish', 'name')
->searchable()
->sortable(),
Column::make('Category', 'category_name', 'newCategories.name')
->searchable()
->sortable(),
];
}
}