This component is part of Zinq Prime and requires a valid license to use.
Zinq TableBuilder is a powerful tool designed to streamline the creation of tables from Eloquent Collections in Laravel and Livewire. With its intuitive API and minimal configuration, TableBuilder allows you to effortlessly display Eloquent models in a clean, responsive format. By leveraging <zinq:table>
, it ensures your tables are fully responsive, accessible, and seamlessly adapt to both light and dark modes.
To start using TableBuilder, you can use BuildsTables
trait in your Livewire component:
use Mattbit\Zinq\Utilities\BuildsTables;
class Payments extends Component
{
use BuildsTables;
...
}
Or you can simply inject TableBuilder
instance:
use Mattbit\Zinq\Builders\TableBuilder;
class Payments extends Component
{
public function render(TableBuilder $tableBuilder)
{
...
}
}
To build a table, you need to call tableFromCollection
method from BuildsTable
trait or fromCollection
method on the builder instance. The method accepts an Eloquent Collection.
ID | Status | Amount | Created at | Updated at |
---|---|---|---|---|
4
|
processing
|
4820
|
Mar 16, 22:28 PM
|
Mar 16, 23:26 PM
|
3
|
success
|
1500
|
Mar 14, 22:02 PM
|
Mar 14, 23:20 PM
|
2
|
failed
|
2000
|
Mar 11, 22:23 PM
|
Mar 11, 23:14 PM
|
1
|
success
|
1000
|
Mar 7, 22:23 PM
|
Mar 7, 23:08 PM
|
use Mattbit\Zinq\Utilities\BuildsTables;
class Payments extends Component
{
use BuildsTables;
public function render(): string
{
$table = $this->tableFromCollection(Payment::all());
return view('livewire.payments', ['table' => $table]);
}
}
Table can be used a string, so you can simply echo it in your blade template:
<div>
{!! $table !!}
</div>
TableBuilder uses Illuminate\Support\Collection
proxy to access collection methods. You can use all methods available in the Collection class.
<div>
@if ($table->isEmpty())
<p>No payments found.</p>
@else
<p>There are @{{ $table->count() }} payments.</p>
{!! $table !!}
@endif
</div>
By default TableBuilder will display all columns from the model and will use the attribute name as a header.
TableBuilder supports hidden attributes, which will not be displayed in the table.
If you wish to hide given attribute without modifying the model, you can use hide
method.
Status | Amount | Created at |
---|---|---|
processing
|
4820
|
Mar 16, 22:28 PM
|
success
|
1500
|
Mar 14, 22:02 PM
|
failed
|
2000
|
Mar 11, 22:23 PM
|
success
|
1000
|
Mar 7, 22:23 PM
|
public function render(): string
{
return $this->tableFromCollection(Payment::all())
->hide('id', 'updated_at') // Hide id and updated_at attributes from the table
->render();
}
TableBuilder will use te Eloquent's getAttribute
method to display the value of the attribute. If you wish to change this behaviour for specific attribute, you can use override
method.
Status | Amount | Created at |
---|---|---|
processing
|
$4,820.00
|
Mar 16, 22:28 PM
|
success
|
$1,500.00
|
Mar 14, 22:02 PM
|
failed
|
$2,000.00
|
Mar 11, 22:23 PM
|
success
|
$1,000.00
|
Mar 7, 22:23 PM
|
public function render(): string
{
return $this->tableFromCollection(Payment::all())
->override('amount', function (Payment $payment) {
// You can use $payment instance to display the value.
return '$' . number_format($payment->amount, 2);
})
->render();
}
TableBuilder formats each attribute to prepare a nice label representation for you. It uppercase first letter, replaces any underscores with whitespaces and even formats id to be present as ID. If you want to display a custom header for given attribute, you can use rename
method.
Status | Amount | Date |
---|---|---|
processing
|
$4,820.00
|
Mar 16, 22:28 PM
|
success
|
$1,500.00
|
Mar 14, 22:02 PM
|
failed
|
$2,000.00
|
Mar 11, 22:23 PM
|
success
|
$1,000.00
|
Mar 7, 22:23 PM
|
public function render(): string
{
return $this->tableFromCollection(Payment::all())
->rename('created_at', 'Date')
->render();
}
Sometimes you need to display additional information in the table, like information from the related models. To fit this scenario, TableBuilder has add
method to add a new column to the table.
Status | Amount | Date | Customer name |
---|---|---|---|
processing
|
$4,820.00
|
Mar 16, 22:28 PM
|
Prof. Dahlia Heaney
|
success
|
$1,500.00
|
Mar 14, 22:02 PM
|
Prof. Jamar Leannon
|
failed
|
$2,000.00
|
Mar 11, 22:23 PM
|
Braxton Goodwin
|
success
|
$1,000.00
|
Mar 7, 22:23 PM
|
Branson Jast
|
public function render(): string
{
return $this->tableFromCollection(Payment::with('user')->get())
->add('customer', fn (Payment $payment) => $payment->customer->name)
->rename('customer', 'Customer name') // You can rename the column as well
->render();
}
If you want to display a show button for each row, you can use link
method. It will create a new column with a link to the given route.
Status | Amount | Date | Customer name | |
---|---|---|---|---|
processing
|
$4,820.00
|
Mar 16, 22:28 PM
|
Prof. Price Bernier
|
|
success
|
$1,500.00
|
Mar 14, 22:02 PM
|
Ursula Hoeger PhD
|
|
failed
|
$2,000.00
|
Mar 11, 22:23 PM
|
Queenie Gutkowski PhD
|
|
success
|
$1,000.00
|
Mar 7, 22:23 PM
|
Corrine Beahan
|
public function render(): string
{
return $this->tableFromCollection(Payment::with('user')->get())
->link(fn (Payment $payment) => route('payments.show', $payment->id))
->render();
}
TableBuilder will format date attributes using format specified in the config/zinq.php
file. You can modify default date format by changing date_format
key in the config file.
Or you can use override
method to change the date format for specific attribute.
TableBuilder will display the columns in the order they are defined in the model. If you want to change the order of the columns, you can use order
method.
Customer name | Status | Amount | Date | |
---|---|---|---|---|
Stephanie Kertzmann II
|
processing
|
$4,820.00
|
Mar 16, 22:28 PM
|
|
Chaim Schiller
|
success
|
$1,500.00
|
Mar 14, 22:02 PM
|
|
Isom Kertzmann
|
failed
|
$2,000.00
|
Mar 11, 22:23 PM
|
|
Fredy Kub PhD
|
success
|
$1,000.00
|
Mar 7, 22:23 PM
|
public function render(): string
{
return $this->tableFromCollection(Payment::all())
->order('customer', 'status', 'amount', 'created_at')
->render();
}
Now we can combine all the methods to create a nice table.
Customer name | Status | Amount | Date | |
---|---|---|---|---|
Rupert Dare
|
Processing
|
$4,820.00
|
Mar 16, 22:28 PM
|
|
Susanna Gaylord
|
Completed
|
$1,500.00
|
Mar 14, 22:02 PM
|
|
Monroe Wolff
|
Failed
|
$2,000.00
|
Mar 11, 22:23 PM
|
|
Prof. Sven Dietrich DDS
|
Completed
|
$1,000.00
|
Mar 7, 22:23 PM
|
public function render(): string
{
return $this->tableFromCollection(Payment::with('user')->get())
->hide('id', 'updated_at')
->add('customer', fn (Payment $payment) => $payment->customer->name)
->override('amount', fn (Payment $payment) => '$' . number_format($payment->amount, 2))
->override('status', fn (Payment $payment) => match ($payment->status) {
'processing' => '<zinq:badge sm info>Processing</zinq:badge>',
'success' => '<zinq:badge sm success>Completed</zinq:badge>',
'failed' => '<zinq:badge sm danger>Failed</zinq:badge>',
default => '<zinq:badge sm>Unknown</zinq:badge>',
})
->rename('created_at', 'Date')
->rename('customer', 'Customer name')
->link(fn (Payment $payment) => route('payments.show', $payment->id))
->order('customer', 'status', 'amount', 'created_at')
->render();
}
We’re constantly expanding the capabilities of Zinq, and some powerful TableBuilder new features are scheduled for release in early 2025:
By purchasing a Zinq license today, you’ll secure lifetime access to all updates, ensuring you can take advantage of these features the moment they’re ready. Don’t miss this opportunity to lock in the promotional pricing now and guarantee your access to the best Zinq has to offer!