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
|
Dec 18, 12:03 PM
|
Dec 18, 13:30 PM
|
3
|
success
|
1500
|
Dec 16, 12:04 PM
|
Dec 16, 13:24 PM
|
2
|
failed
|
2000
|
Dec 13, 12:34 PM
|
Dec 13, 13:18 PM
|
1
|
success
|
1000
|
Dec 9, 12:01 PM
|
Dec 9, 13:12 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
|
Dec 18, 12:03 PM
|
success
|
1500
|
Dec 16, 12:04 PM
|
failed
|
2000
|
Dec 13, 12:34 PM
|
success
|
1000
|
Dec 9, 12:01 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
|
Dec 18, 12:03 PM
|
success
|
$1,500.00
|
Dec 16, 12:04 PM
|
failed
|
$2,000.00
|
Dec 13, 12:34 PM
|
success
|
$1,000.00
|
Dec 9, 12:01 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
|
Dec 18, 12:03 PM
|
success
|
$1,500.00
|
Dec 16, 12:04 PM
|
failed
|
$2,000.00
|
Dec 13, 12:34 PM
|
success
|
$1,000.00
|
Dec 9, 12:01 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
|
Dec 18, 12:03 PM
|
Lemuel Padberg V
|
success
|
$1,500.00
|
Dec 16, 12:04 PM
|
Celestino Jast I
|
failed
|
$2,000.00
|
Dec 13, 12:34 PM
|
Chet Torp
|
success
|
$1,000.00
|
Dec 9, 12:01 PM
|
Asha Cruickshank
|
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
|
Dec 18, 12:03 PM
|
Lon Mills
|
|
success
|
$1,500.00
|
Dec 16, 12:04 PM
|
Rory Pacocha
|
|
failed
|
$2,000.00
|
Dec 13, 12:34 PM
|
Wilber Lind
|
|
success
|
$1,000.00
|
Dec 9, 12:01 PM
|
Prof. Simeon Orn
|
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 | |
---|---|---|---|---|
Marc Hackett
|
processing
|
$4,820.00
|
Dec 18, 12:03 PM
|
|
Deion Runte
|
success
|
$1,500.00
|
Dec 16, 12:04 PM
|
|
Shaun Hackett
|
failed
|
$2,000.00
|
Dec 13, 12:34 PM
|
|
Mr. Deshaun Mayert
|
success
|
$1,000.00
|
Dec 9, 12:01 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 | |
---|---|---|---|---|
Ms. Vilma Durgan
|
Processing
|
$4,820.00
|
Dec 18, 12:03 PM
|
|
Chaya Fay
|
Completed
|
$1,500.00
|
Dec 16, 12:04 PM
|
|
Pascale Treutel
|
Failed
|
$2,000.00
|
Dec 13, 12:34 PM
|
|
Newell O'Connell
|
Completed
|
$1,000.00
|
Dec 9, 12:01 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!