DataSource โ
Configuring the DataSource โ
datasource() must return one of these types:
| Type | Description |
|---|---|
| Eloquent Builder | Pulls data from Laravel models. |
| Query Builder | Uses manual SQL-style queries. |
Collection / Array | Data stored in memory and managed in your code. |
| Laravel Scout Builder | Full-text search via Scout (Model::search()). |
Eloquent โ
Return an Eloquent Builder to retrieve records from a Model.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Product;
class ProductTable extends PowerGridComponent
{
public function datasource(): Builder
{
return Product::query();
}
}Query Builder โ
Return a Query Builder instance when you query the database without a Model.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
class ProductTable extends PowerGridComponent
{
public function datasource(): Builder
{
return DB::table('products');
}
}Collection โ
Return a Laravel Collection or an array. Useful for static data or data already loaded in memory.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Support\Collection;
class ProductTable extends PowerGridComponent
{
public function datasource(): Collection
{
return collect([
[
'id' => 1,
'name' => 'Pepperoni Pizza',
'kitchen_name' => 'Pizza Station',
'price' => 14.00,
],
[
'id' => 2,
'name' => 'Bacalhau com Natas',
'kitchen_name' => 'Main Kitchen',
'price' => 16.50,
],
[
'id' => 3,
'name' => 'Sparkling Water',
'kitchen_name' => 'Beverages Bar',
'price' => 2.50,
],
]);
}
}Scout โ
When Laravel Scout is installed, return a Scout Builder. PowerGrid applies the table Search Input to that builder โ do not pass $this->search into search() yourself.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Laravel\Scout\Builder as ScoutBuilder;
use App\Models\Product;
class ProductTable extends PowerGridComponent
{
public function datasource(): ScoutBuilder
{
return Product::search();
}
}Loading Relationships โ
Some features, such as sortable() and column filters, need the related table's columns in the same result row. Join those tables in datasource(), or eager-load relations when you only need them for custom fields.
To search related models without a join, see relationSearch().
Loading Eloquent Relationships โ
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Product;
class ProductTable extends PowerGridComponent
{
public function datasource(): Builder
{
return Product::query()->with('kitchen');
}
}The Product Model might look something like this:
class Product extends Model
{
public function kitchen(): BelongsTo
{
return $this->belongsTo(Kitchen::class);
}
}Joining Tables with Eloquent Builder โ
This example retrieves products together with their kitchen name by joining the kitchens table.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Product;
class ProductTable extends PowerGridComponent
{
public function datasource(): Builder
{
return Product::query()
->join('kitchens', function ($kitchens) {
$kitchens->on('products.kitchen_id', '=', 'kitchens.id');
})
->select('products.*', 'kitchens.name as kitchen_name');
}
}Joining Tables with Query Builder โ
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query\Builder;
class ProductTable extends PowerGridComponent
{
public function datasource(): Builder
{
return DB::table('products')
->join('kitchens', function ($kitchens) {
$kitchens->on('products.kitchen_id', '=', 'kitchens.id');
})
->select([
'products.id',
'products.name',
'products.price',
'kitchens.name as kitchen_name',
]);
}
}Defining DataSource Fields โ
fields() maps datasource values for your columns. Call PowerGrid::fields() and chain add() for each field. Without a closure, PowerGrid reads the attribute of the same name.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
class ProductTable extends PowerGridComponent
{
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('price');
}
}Retrieving Relationship Fields โ
Eloquent Fields โ
Use a closure to read a related model. This example returns the related kitchen name as kitchen_name.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
use App\Models\Product;
class ProductTable extends PowerGridComponent
{
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('kitchen_name', fn (Product $product) => e($product->kitchen->name));
}
}Join Fields โ
Fields from the joined table are available under the alias assigned in the query. Here kitchens.name is kitchen_name.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
class ProductTable extends PowerGridComponent
{
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('price')
->add('kitchen_name');
}
}Formatting Data โ
To modify data before it is displayed, see Formatting Data.
Custom Primary Key โ
$primaryKey and $sortField default to id. Change both when your key column differs, or when joined tables both expose id. If the SQL key is qualified (for example products.id) but the row attribute is not, also set $primaryKeyAlias.
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class ProductTable extends PowerGridComponent
{
public string $primaryKey = 'products.custom_product_id';
public ?string $primaryKeyAlias = 'custom_product_id';
public string $sortField = 'products.custom_product_id';
}