Workflow
Datasource Join

This example demonstrates the use of a Join Relationship in a Datasource.

ID
Dish
Category
1
Arkansas Possum Pie
Pasta
2
Albacore Tuna Melt
Pie
3
Π±ΠΎΡ€Ρ‰
Soup
4
Bacalhau com natas
Dessert
5
Baba Ghanoush
Pasta
6
Bacon Cheeseburger
Pasta
7
Baked potato
Meat
8
Baklava
Pie
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\DatasourceJoinTable;
 
use App\Models\Dish;
use Illuminate\Database\Eloquent\Builder;
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 DatasourceJoinTable extends PowerGridComponent
{
public function setUp(): array
{
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->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('dish_name', fn ($dish) => e($dish->name))
->add('category_name', fn ($dish) => e($dish->category->name));
}
 
public function columns(): array
{
return [
Column::make('ID', 'id')
->searchable()
->sortable(),
 
Column::make('Dish', 'dish_name', 'dishes.name')
->searchable()
->sortable(),
 
Column::make('Category', 'category_name', 'newCategories.name')
->searchable()
->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.4.12
A front-end framework for Laravel.
openspout/openspout
v4.24.0
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
v5.6.3
PowerGrid generates Advanced Datatables using Laravel Livewire.