Skip to content

Commit dbad2dd

Browse files
authored
Merge branch 'filamentphp:main' into main
2 parents 832e51a + 58cad90 commit dbad2dd

File tree

416 files changed

+25836
-14460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

416 files changed

+25836
-14460
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\Author;
7+
8+
class AuthorController extends Controller
9+
{
10+
public function index()
11+
{
12+
return Author::paginate();
13+
}
14+
15+
public function show(Author $author)
16+
{
17+
return $author->append('stars_count');
18+
}
19+
}

app/Models/Author.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Casts\Attribute;
67
use Illuminate\Database\Eloquent\Factories\HasFactory;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -63,6 +64,13 @@ public function getStarsCount(): int
6364
);
6465
}
6566

67+
protected function starsCount(): Attribute
68+
{
69+
return Attribute::make(
70+
get: fn () => $this->getStarsCount(),
71+
);
72+
}
73+
6674
public function cacheStarsCount(): void
6775
{
6876
cache()->forget($this->getStarsCountCacheKey());

content/articles/leandrocfe-brazilian-cpfcnpj-field.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Brazilian CPF CNPJ fields
3-
slug: brazilian-cpfcnpj-field
3+
slug: leandrocfe-brazilian-cpfcnpj-field
44
author_slug: leandrocfe
55
publish_date: 2023-03-01
66
categories: [form-builder, alpinejs]
@@ -87,4 +87,4 @@ TextInput::make('cpf')
8787
TextInput::make('cnpj')
8888
->extraAlpineAttributes(['x-mask' => '99.999.999/9999-99'])
8989
->rule('cnpj')
90-
```
90+
```

content/articles/leandrocfe-brazilian-phone-number-field.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Brazilian Phone Number field
3-
slug: brazilian-phone-number-field
3+
slug: leandrocfe-brazilian-phone-number-field
44
author_slug: leandrocfe
55
publish_date: 2022-12-05
66
categories: [form-builder, alpinejs]
@@ -45,4 +45,4 @@ public function boot()
4545
```php
4646
TextInput::make('phone_number')
4747
->extraAlpineAttributes(['x-mask:dynamic' => '$input.length >=14 ? \'(99)99999-9999\' : \'(99)9999-9999\''])
48-
```
48+
```

content/articles/leandrocfe-easy-way-to-create-a-filament-theme-in-minutes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Easy way to create a Filament Theme in minutes!
3-
slug: easy-way-to-create-a-filament-theme-in-minutes
3+
slug: leandrocfe-easy-way-to-create-a-filament-theme-in-minutes
44
author_slug: leandrocfe
55
publish_date: 2022-10-21
66
categories: [panel-builder, tailwind-css]
@@ -311,4 +311,4 @@ npm run build && php artisan serve
311311

312312
Once you have started the Artisan development server, your application will be accessible in your web browser at <http://127.0.0.1:8000/admin>. You can choose a user's credentials and authenticate to access the Filament Admin Panel (default password: **password**).
313313

314-
You can download this project on GitHub: [leandrocfe/filament-custom-theme](https://github.com/leandrocfe/filament-custom-theme)
314+
You can download this project on GitHub: [leandrocfe/filament-custom-theme](https://github.com/leandrocfe/filament-custom-theme)

content/articles/leandrocfe-how-to-consume-an-external-api-with-filament-tables.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: How to consume an external API with Filament Tables
3-
slug: how-to-consume-an-external-api-with-filament-tables
3+
slug: leandrocfe-how-to-consume-an-external-api-with-filament-tables
44
author_slug: leandrocfe
55
publish_date: 2022-10-28
66
categories: [panel-builder, table-builder, integration]
@@ -235,4 +235,4 @@ protected function getActions(): array
235235

236236
Visit your **Product Resource** at **/admin/products** to try it! Hope you enjoy!
237237

238-
You can download this project on GitHub: <https://github.com/leandrocfe/filament-tables-json-data-source>
238+
You can download this project on GitHub: <https://github.com/leandrocfe/filament-tables-json-data-source>

content/articles/leandrocfe-how-to-refresh-widgets-when-table-actions-are-fired.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: How to Refresh Widgets When Table Actions Are Fired
3-
slug: how-to-refresh-widgets-when-table-actions-are-fired
3+
slug: leandrocfe-how-to-refresh-widgets-when-table-actions-are-fired
44
author_slug: leandrocfe
55
publish_date: 2023-03-04
66
categories: [livewire, panel-builder, table-builder]

content/articles/leandrocfe-how-to-write-tests-for-filament-admin-panels.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: How to write tests for Filament admin panels
3-
slug: how-to-write-tests-for-filament-admin-panels
3+
slug: leandrocfe-how-to-write-tests-for-filament-admin-panels
44
author_slug: leandrocfe
55
publish_date: 2022-11-06
66
categories: [laravel, livewire, panel-builder, table-builder, form-builder]
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
title: Navigating Filament Pages with Previous and Next Buttons
3+
slug: leandrocfe-navigating-filament-pages-with-previous-and-next-buttons
4+
author_slug: leandrocfe
5+
publish_date: 2024-07-26
6+
categories: [livewire, panel-builder]
7+
type_slug: article
8+
---
9+
10+
![Example](https://github.com/leandrocfe/article-fi-page-nav/blob/d36b3b7225adaace0aeb386455f6f1420f16f3d8/screenshots/example.gif?raw=true)
11+
12+
13+
## Introduction
14+
15+
In this article, we will delve into the process of crafting previous and next navigation buttons within Filament pages. These buttons, designed as Filament Header Actions, can seamlessly navigate through ViewPages and EditPages.
16+
17+
## Implementing New Actions
18+
19+
To kick things off, we need to introduce two new actions: PreviousAction and NextAction. These actions will reside in the `app/Filament/Resources/Actions` folder.
20+
21+
**PreviousAction.php**
22+
23+
```php
24+
//app/Filament/Resources/Actions/PreviousAction.php
25+
namespace App\Filament\Resources\Actions;
26+
27+
use Filament\Actions\Action;
28+
29+
class PreviousAction extends Action
30+
{
31+
public static function getDefaultName(): ?string
32+
{
33+
return 'previous';
34+
}
35+
36+
protected function setUp(): void
37+
{
38+
parent::setUp();
39+
40+
$this->hiddenLabel()
41+
->icon('heroicon-o-arrow-left')
42+
->outlined()
43+
->tooltip("Previous {$this->getRecordTitle()}");
44+
}
45+
}
46+
```
47+
48+
**NextAction.php**
49+
50+
```php
51+
//app/Filament/Resources/Actions/NextAction.php
52+
namespace App\Filament\Resources\Actions;
53+
54+
use Filament\Actions\Action;
55+
56+
class NextAction extends Action
57+
{
58+
public static function getDefaultName(): ?string
59+
{
60+
return 'next';
61+
}
62+
63+
protected function setUp(): void
64+
{
65+
parent::setUp();
66+
67+
$this->hiddenLabel()
68+
->icon('heroicon-o-arrow-right')
69+
->outlined()
70+
->tooltip("Next {$this->getRecordTitle()}");
71+
}
72+
}
73+
```
74+
75+
## Configuring Actions in Pages
76+
77+
Next, we extend the capabilities of the ViewRecord and EditRecord classes by adding a `CanPaginateViewRecord` trait. This trait, residing in the `app/Filament/Resources/Pages/Concerns` folder, configures actions for pagination and provides methods to retrieve previous and next records based on the current record.
78+
79+
```bash
80+
# Laravel 11 and higher
81+
php artisan make:trait Filament/Resources/Pages/Concerns/CanPaginateViewRecord
82+
83+
# Laravel 10 create it manually
84+
```
85+
86+
```php
87+
namespace App\Filament\Resources\Pages\Concerns;
88+
89+
use App\Filament\Resources\Actions\NextAction;
90+
use App\Filament\Resources\Actions\PreviousAction;
91+
use Filament\Actions\Action;
92+
use Illuminate\Database\Eloquent\Model;
93+
94+
trait CanPaginateViewRecord
95+
{
96+
protected function configureAction(Action $action): void
97+
{
98+
$this->configureActionRecord($action);
99+
100+
match (true) {
101+
$action instanceof PreviousAction => $this->configurePreviousAction($action),
102+
$action instanceof NextAction => $this->configureNextAction($action),
103+
default => parent::configureAction($action),
104+
};
105+
}
106+
107+
protected function configurePreviousAction(Action $action): void
108+
{
109+
if ($this->getPreviousRecord()) {
110+
$action->url(fn (): string => static::getResource()::getUrl(static::getResourcePageName(), ['record' => $this->getPreviousRecord()]));
111+
} else {
112+
$action
113+
->disabled()
114+
->color('gray');
115+
}
116+
}
117+
118+
protected function configureNextAction(Action $action): void
119+
{
120+
if ($this->getNextRecord()) {
121+
$action->url(fn (): string => static::getResource()::getUrl(static::getResourcePageName(), ['record' => $this->getNextRecord()]));
122+
} else {
123+
$action
124+
->disabled()
125+
->color('gray');
126+
}
127+
}
128+
129+
protected function getPreviousRecord(): ?Model
130+
{
131+
return $this
132+
->getRecord()
133+
->where('id', '<', $this->getRecord()->id)
134+
->orderBy('id', 'desc')
135+
->first();
136+
}
137+
138+
protected function getNextRecord(): ?Model
139+
{
140+
return $this
141+
->getRecord()
142+
->where('id', '>', $this->getRecord()->id)
143+
->orderBy('id', 'asc')
144+
->first();
145+
}
146+
}
147+
```
148+
149+
> **_NOTE:_** In this example, we use auto-incrementing IDs for the tables. If your tables are configured differently, you’ll need to adjust the `getPreviousRecord` and `getNextRecord` methods accordingly.
150+
151+
## Usage Example
152+
153+
Now, let's implement these actions in the **ViewRecord** and **EditRecord** pages. By including the `CanPaginateViewRecord` trait and registering the actions in the `getHeaderActions` array, you can enable previous and next navigation buttons. Below is an example using the ViewPost page:
154+
155+
```php
156+
namespace App\Filament\Resources\PostResource\Pages;
157+
158+
use App\Filament\Resources\Actions\NextAction;
159+
use App\Filament\Resources\Actions\PreviousAction;
160+
use App\Filament\Resources\Pages\Concerns\CanPaginateViewRecord;
161+
use App\Filament\Resources\PostResource;
162+
use Filament\Actions;
163+
use Filament\Resources\Pages\ViewRecord;
164+
165+
class ViewPost extends ViewRecord
166+
{
167+
use CanPaginateViewRecord;
168+
169+
protected static string $resource = PostResource::class;
170+
171+
protected function getHeaderActions(): array
172+
{
173+
return [
174+
Actions\EditAction::make(),
175+
PreviousAction::make(),
176+
NextAction::make(),
177+
];
178+
}
179+
}
180+
```
181+
## Conclusion
182+
183+
This project, including all the provided code, is available on [GitHub](https://github.com/leandrocfe/article-fi-page-nav).
184+
185+
We hope you find this tutorial helpful in enhancing navigation within your Filament pages. Happy coding!
456 KB
Loading

0 commit comments

Comments
 (0)