Tables
You can use tables to extend the admin panel by writing PHP and never touching TypeScript or CSS. This page covers how to create tables and then how to display them in the admin panel.
Defining a table
Use the following command to scaffold the code for a table:
1php artisan make:larasell-table ProductReviewTableThe command creates the following code:
1namespace App\Tables;
2
3use Larasell\Admin\Tables\Table;
4
5class ProductReviewTable extends Table
6{
7 public function columns(): array
8 {
9 return [
10 //
11 ];
12 }
13}The UseModel attribute sets the Eloquent model the table lists. You can also set the model with a $model property:
1protected ?string $model = ProductReview::class;Adding columns
To add columns, use
the TextColumn class:
1namespace App\Tables;
2
3use Larasell\Admin\Tables\Columns\TextColumn;
4use Larasell\Admin\Tables\Table;
5
6class ProductReviewTable extends Table
7{
8 public function columns(): array
9 {
10 return [
11 TextColumn::make('author'),
12 TextColumn::make('rating'),
13 TextColumn::make('body', 'Review'),
14 ];
15 }
16}When you pass the make method a string, the column shows the property of the Eloquent model.
The columns appear in the order you add them. To re-order them, move the classes around in the array.
Labelling columns
The second parameter allows you to define a label. The label is optional.
When you don't pass a label, Table infers it. author turns into "Author" and
multi-word strings, like product-review, product_review or productReview, turn
into "Product review".
Formatting values
Use map to change what a column shows. The callback receives the value and the record:
1TextColumn::make('rating')->map(fn (int $rating) => str_repeat('★', $rating)),
2TextColumn::make('author')->map(fn ($value, ProductReview $review) => $review->user->name),The column doesn't have to be an attribute of the model, so you can compute a column from other attributes.
The callback must return text, a number or null. Anything else, like a boolean, throws an exception.
Adding images
Use image to show an icon or a picture before a cell's value. The callback receives the record
and an Image, and returns the image to show:
1use App\Models\ProductReview;
2use Larasell\Admin\Enums\Icon;
3use Larasell\Admin\Tables\Columns\Image;
4use Larasell\Admin\Tables\Columns\TextColumn;
5use Larasell\Admin\Tables\Table;
6
7class ProductReviewTable extends Table
8{
9 public function columns(): array
10 {
11 return [
12 TextColumn::make('author')
13 ->image(function (ProductReview $review, Image $image) {
14 return $image
15 ->url($review->user->avatar_url)
16 ->rounded();
17 }),
18 TextColumn::make('rating')
19 ->image(function (ProductReview $review, Image $image) {
20 return $image->icon(Icon::Star);
21 }),
22 ];
23 }
24}icon accepts an Icon enum case or a Remix icon name.
url takes the address of a picture. To show a picture your app serves, use route,
signedRoute or temporarySignedRoute. They take the same arguments as Laravel's URL helpers of
the same name:
1// A picture at a named route
2$image->route('avatars.show', $review->user);
3
4// A picture behind a signed URL
5$image->signedRoute('avatars.show', $review->user);
6
7// A picture behind a signed URL that expires after an hour
8$image->temporarySignedRoute(
9 'avatars.show',
10 now()->addHour(),
11 $review->user,
12);Pictures are square. Call rounded to show them as a circle, for example for avatars.
Return null to show no image for a row:
1TextColumn::make('author')
2 ->image(function (ProductReview $review, Image $image) {
3 if ($review->user->avatar_url === null) {
4 return null;
5 }
6
7 return $image
8 ->url($review->user->avatar_url)
9 ->rounded();
10 }),When an image tells the user something the text doesn't, describe it with alt for screen
readers. Use title to show a tooltip when the user hovers over the image:
1$image
2 ->icon(Icon::Star)
3 ->alt('Verified purchase')
4 ->title('Verified purchase');Displaying the table
Use the index method of the Navigation facade inside routes/admin.php
to show the table on its own page and link to it from the sidebar:
1use App\Tables\ProductReviewTable;
2use Larasell\Admin\Enums\Icon;
3use Larasell\Admin\Facades\Navigation;
4
5Navigation::index(
6 'Reviews',
7 'reviews',
8 ProductReviewTable::class,
9 Icon::Star,
10);It takes a label, a URI, the table class and an icon. The table shows up at /admin/reviews,
on a route named larasell.admin.reviews.index, and the sidebar gets a "Reviews" item that
links to it.
Adding the table to the navigation is optional. To only register the page, and link to it
yourself like any other route, use Route::index instead. The
navigation reference shows how.
Row links
A table can't show everything a user needs to see about a record. Link each row to that record's detail page:
1use App\Models\ProductReview;
2use Larasell\Admin\Tables\Table;
3
4class ProductReviewTable extends Table
5{
6 // ...
7
8 public function rowLink(ProductReview $productReview): ?string
9 {
10 return route('reviews.show', $productReview);
11 }
12}Return null to leave a row unlinked:
1public function rowLink(ProductReview $productReview): ?string
2{
3 if (! $productReview->is_published) {
4 return null;
5 }
6
7 return route('reviews.show', $productReview);
8}Header actions
Some actions belong to the whole table rather than one row, like adding a new record.
Return them from the headerActions method and Larasell shows them above the table:
1use Larasell\Admin\Enums\Icon;
2use Larasell\Admin\Tables\Actions\TableAction;
3use Larasell\Admin\Tables\Table;
4
5class ProductReviewTable extends Table
6{
7 // ...
8
9 public function headerActions(): array
10 {
11 return [
12 TableAction::make('Add review')
13 ->route('reviews.create')
14 ->icon(Icon::Add),
15 ];
16 }
17}A header action is a link. Use route to link to a named route, or to to link to any URL.
The icon method works the same as on row actions.
Header actions are hidden while the table shows its empty state. To offer
them in the empty state too, return them from the actions key:
1use Larasell\Admin\Tables\Table;
2
3class ProductReviewTable extends Table
4{
5 // ...
6
7 public function emptyState(): ?array
8 {
9 return [
10 'title' => 'No reviews yet',
11 'actions' => $this->headerActions(),
12 ];
13 }
14}Table title
Each table has a title that Larasell displays. To customise the title, use the title method:
1use Larasell\Admin\Tables\Table;
2
3class ProductReviewTable extends Table
4{
5 // ...
6
7 public function title(): string
8 {
9 return 'My table';
10 }
11}The title is optional. Table infers the
name based on the name of the class. ProductReviewTable turns into "Product review".
Empty states
Larasell adds an empty state to each table.
Use the emptyState method to customise the empty state. It takes
a title, description and an icon:
1use Larasell\Admin\Enums\Icon;
2use Larasell\Admin\Tables\Table;
3
4class ProductReviewTable extends Table
5{
6 // ...
7
8 public function emptyState(): ?array
9 {
10 return [
11 'title' => 'No reviews yet',
12 'description' => 'Reviews will show up here as soon as customers leave them.',
13 'icon' => Icon::Star,
14 ];
15 }
16}