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 29, 6:40 AM
|
Mar 29, 7:24 AM
|
3
|
success
|
1500
|
Mar 27, 6:43 AM
|
Mar 27, 7:18 AM
|
2
|
failed
|
2000
|
Mar 24, 6:34 AM
|
Mar 24, 7:12 AM
|
1
|
success
|
1000
|
Mar 20, 6:38 AM
|
Mar 20, 7:06 AM
|
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 29, 6:40 AM
|
success
|
1500
|
Mar 27, 6:43 AM
|
failed
|
2000
|
Mar 24, 6:34 AM
|
success
|
1000
|
Mar 20, 6:38 AM
|
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 29, 6:40 AM
|
success
|
$1,500.00
|
Mar 27, 6:43 AM
|
failed
|
$2,000.00
|
Mar 24, 6:34 AM
|
success
|
$1,000.00
|
Mar 20, 6:38 AM
|
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 29, 6:40 AM
|
success
|
$1,500.00
|
Mar 27, 6:43 AM
|
failed
|
$2,000.00
|
Mar 24, 6:34 AM
|
success
|
$1,000.00
|
Mar 20, 6:38 AM
|
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 29, 6:40 AM
|
Aniya Jenkins
|
success
|
$1,500.00
|
Mar 27, 6:43 AM
|
Wilfredo Runolfsson
|
failed
|
$2,000.00
|
Mar 24, 6:34 AM
|
Jocelyn Upton
|
success
|
$1,000.00
|
Mar 20, 6:38 AM
|
Prof. Laney Carroll I
|
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 29, 6:40 AM
|
Skye Wiza
|
|
success
|
$1,500.00
|
Mar 27, 6:43 AM
|
Ms. Elenora Lueilwitz DDS
|
|
failed
|
$2,000.00
|
Mar 24, 6:34 AM
|
Angela Schuppe
|
|
success
|
$1,000.00
|
Mar 20, 6:38 AM
|
Marlin Kreiger I
|
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 | |
---|---|---|---|---|
Wilbert Goyette DDS
|
processing
|
$4,820.00
|
Mar 29, 6:40 AM
|
|
Trycia Romaguera
|
success
|
$1,500.00
|
Mar 27, 6:43 AM
|
|
Madelynn Kuvalis
|
failed
|
$2,000.00
|
Mar 24, 6:34 AM
|
|
Cordia Ziemann
|
success
|
$1,000.00
|
Mar 20, 6:38 AM
|
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 | |
---|---|---|---|---|
Loraine Schiller
|
Processing
|
$4,820.00
|
Mar 29, 6:40 AM
|
|
Karli Rau
|
Completed
|
$1,500.00
|
Mar 27, 6:43 AM
|
|
Brett Gleichner
|
Failed
|
$2,000.00
|
Mar 24, 6:34 AM
|
|
Keshawn Nikolaus
|
Completed
|
$1,000.00
|
Mar 20, 6:38 AM
|
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!