Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit b543f3e

Browse files
lartisanCristian Iosifpascalbaljet
authored
Using @forelse to add message when the table has no resources to display (#267)
Co-authored-by: Cristian Iosif <[email protected]> Co-authored-by: Pascal Baljet <[email protected]>
1 parent 178816f commit b543f3e

File tree

6 files changed

+64
-6
lines changed

6 files changed

+64
-6
lines changed

app/app/Http/Controllers/TableController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ public function caseInsensitive()
8181
]);
8282
}
8383

84+
public function empty()
85+
{
86+
SpladeTable::hidePaginationWhenResourceContainsOnePage();
87+
88+
return view('table.users', [
89+
'users' => SpladeTable::for(User::query()->where('id', '<', 1))
90+
->withGlobalSearch(columns: ['name'])
91+
->column('name')
92+
->ignoreCase(true)
93+
->paginate(10),
94+
]);
95+
}
96+
8497
public function overflow(bool $spladeQueryBuilder = false)
8598
{
8699
$query = User::query()->orderBy('name');

app/routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@
309309
Route::get('/caseSensitive/{spladeQueryBuilder?}', [TableController::class, 'caseSensitive'])->name('table.caseSensitive');
310310
Route::get('/caseInsensitive/{spladeQueryBuilder?}', [TableController::class, 'caseInsensitive'])->name('table.caseInsensitive');
311311

312+
Route::get('/empty', [TableController::class, 'empty'])->name('table.empty');
313+
312314
Route::get('/preserveScrollForm', [TableController::class, 'preserveScrollForm'])->name('table.preserveScrollForm');
313315
Route::post('/preserveScrollForm', [TableController::class, 'preserveScrollFormSubmit'])->name('table.preserveScrollFormSubmit');
314316

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Browser\Table;
4+
5+
use Laravel\Dusk\Browser;
6+
use Tests\DuskTestCase;
7+
8+
/**
9+
* @group table
10+
*/
11+
class EmptyTest extends DuskTestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it_shows_a_text_when_no_results_have_been_found()
17+
{
18+
$this->browse(function (Browser $browser) {
19+
$browser
20+
->visit('/table/empty/')
21+
->assertSee('There are no items to show.');
22+
});
23+
}
24+
}

resources/lang/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"Search": "",
1919
"Select all on this page": "",
2020
"Select all results": "",
21+
"The password confirmation has expired.": "",
22+
"There are no items to show.": "",
2123
"of": "",
2224
"per page": "",
2325
"results": "",

resources/views/table/body.blade.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<tbody class="divide-y divide-gray-200 bg-white">
2-
@foreach($table->resource as $itemKey => $item)
2+
@forelse($table->resource as $itemKey => $item)
33
@php $itemPrimaryKey = $table->findPrimaryKey($item) @endphp
44

55
<tr
@@ -39,5 +39,17 @@ class="whitespace-nowrap text-sm @if($loop->first && $hasBulkActions) pr-6 @else
3939
</td>
4040
@endforeach
4141
</tr>
42-
@endforeach
43-
</tbody>
42+
@empty
43+
<tr>
44+
<td colspan="{{ $table->columns()->count() }}" class="whitespace-nowrap">
45+
@if(isset($emptyState) && !!$emptyState)
46+
{{ $emptyState }}
47+
@else
48+
<p class="text-gray-700 px-6 py-12 font-medium text-sm text-center">
49+
{{ __('There are no items to show.') }}
50+
</p>
51+
@endif
52+
</td>
53+
</tr>
54+
@endforelse
55+
</tbody>

src/Components/Table.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ public function __construct(
4040
*/
4141
public function showPaginator(): bool
4242
{
43-
if (SpladeTable::hidesPaginationWhenResourceContainsOnePage()) {
43+
$resource = $this->for->resource;
44+
45+
$paginator = $resource instanceof Paginator || $resource instanceof CursorPaginator;
46+
47+
if (!$paginator) {
4448
return false;
4549
}
4650

47-
return $this->for->resource instanceof Paginator
48-
|| $this->for->resource instanceof CursorPaginator;
51+
return SpladeTable::hidesPaginationWhenResourceContainsOnePage()
52+
? $resource->hasPages()
53+
: true;
4954
}
5055

5156
/**

0 commit comments

Comments
 (0)