Filament 除了原本已經有的客製化功能以外,還有許多的套件,可以協助我們進行更廣泛的客製化
裡面有許多的套件是免費的,甚至還有 Filament 官方自己開發的套件。
今天我們來介紹幾個比較常用的套件
標籤是一個很常用的功能,當然自己做一個標籤資料表並不難
不過有別人寫好的功能,使用起來就更輕鬆了!
Laravel Tags 就是一個做得很完整的套件
要將這個套件整合進到 Filament 內,可以直接使用 Spatie Tags
首先我們先安裝
composer require filament/spatie-laravel-tags-plugin
安裝好之後,我們建立需要的檔案
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"
如果想做更細的設置,也可以同時建立需要的 config 設定檔
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config"
建立好檔案之後,我們要做一次 migrate 設置好資料庫
如果你的專案是從前面一路製作過來,要記得把原本的 tags 資料表跟 Model 移除,不然會跟 Laravel Tag 的資料表產生衝突
php artisan migrate
安裝好之後,可以看到多了 tags
和 taggable
這兩個資料表。
要能在商品內使用標籤,我們要調整一下原本的 Product Model
use Spatie\Tags\HasTags;
class Product extends Model
{
/** @use HasFactory<\Database\Factories\ProductFactory> */
use HasFactory;
use HasTags;
...
要在後台加上標籤,我們則只需要使用套件提供的 SpatieTagsInput
SpatieTagsInput::make('tags'),
我們就可以看到能自己任意增加標籤的輸入畫面了
這個套件雖然沒有出現在 Filament 的官網內,但是之前客製化時 Laravel Permission 這個套件實在太重要了,所以特別提出來
這個套件可以允許我們實作「基於角色的存取控制」(Role-Based Access Control,RBAC)。在一些結構比較複雜的團隊內,這個權限控管非常重要。
首先我們先安裝好套件
composer require spatie/laravel-permission
接著建立對應的資料庫 migration 和 config 檔案
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
Copying file [vendor/spatie/laravel-permission/config/permission.php] to [config/permission.php] DONE
Copying file [vendor/spatie/laravel-permission/database/migrations/create_permission_tables.php.stub] to [database/migrations/2025_09_13_143243_create_permission_tables.php] DONE
最後做一次 migrate
php artisan migrate
能看到 permissions
這個資料表,那我們就安裝完成了
然後我們在 User
內增加 Spatie\Permission\Traits\HasRoles
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
use HasRoles;
...
之後就可以用 Laravel Permission 控管使用者的權限了
在 Filament 4 之後,控制權限可以使用 Laravel 的 Policy
我們先建立好 ProductPolicy
php artisan make:policy ProductPolicy --model Product
建立好之後,我們就會看到 ProductPolicy
類別
<?php
namespace App\Policies;
use App\Models\Product;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class ProductPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return false;
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Product $product): bool
{
return false;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return false;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Product $product): bool
{
return false;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Product $product): bool
{
return false;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Product $product): bool
{
return false;
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Product $product): bool
{
return false;
}
}
在這邊設定好權限之後,Filament 就可以自動更新權限了!
有關 Filament 可以介紹的東西還很多,希望各位讀者不要停在這些介紹內,可以自己實際運作看看,體驗一下輕鬆客製化後台的快樂!
我們明天見!