Text::make('Name')->showOnIndex(); // 顯示在 Index 頁面 Text::make('Name')->showOnDetail(); // 顯示在 Detail 頁面 Text::make('Name')->showOnCreating(); // 顯示在 Create 表單 Text::make('Name')->showOnUpdating(); // 顯示在 Update 表單 Text::make('Description')->hideFromIndex(); // 隱藏於 Index Text::make('Secret Code')->hideFromDetail(); // 隱藏於 Detail Text::make('Created At')->hideWhenCreating(); // 隱藏於 Create Text::make('ID')->hideWhenUpdating(); // 隱藏於 Update Text::make('Order Status')->onlyOnIndex(); // 僅顯示於 Index Text::make('API Key')->onlyOnDetail(); // 僅顯示於 Detail Text::make('Password')->onlyOnForms(); // 僅顯示於 Create / Update 表單 Text::make('ID')->exceptOnForms(); // 除表單外都顯示
Text::make('Name')->showOnIndex(function () { return $this->name === 'Taylor Otwell'; }); Text::make('Name')->hideFromIndex(function () { return $this->name === 'Taylor Otwell'; });
BelongsTo::make('User')->default($request->user()->getKey()); Text::make('Uuid')->default(function ($request) { return Str::orderedUuid(); });
Text::make('Name')->placeholder('請輸入名稱');
use Laravel\Nova\Panel; public function fields(Request $request) { return [ ID::make()->sortable(), new Panel('Address Information', $this->addressFields()), ]; } protected function addressFields() { return [ Place::make('Address', 'address_line_1')->hideFromIndex(), Text::make('Address Line 2')->hideFromIndex(), Text::make('City')->hideFromIndex(), Text::make('State')->hideFromIndex(), Text::make('Postal Code')->hideFromIndex(), Country::make('Country')->hideFromIndex(), ]; }
(new Panel('Profile', [ Text::make('Full Name'), Date::make('Date of Birth'), Text::make('Place of Birth'), ]))->limit(1);
use Laravel\Nova\Fields\Avatar; Avatar::make('Profile Picture', 'profile_picture') ->disk('public') // 儲存在 storage/app/public ->path('avatars') // 存放於 avatars 資料夾 ->prunable() // 刪除資料時一併刪除圖片 ->maxWidth(250) // 限制圖片最大寬度 ->acceptedTypes('image/jpeg,image/png') // 限制可上傳格式 ->default('/images/default-avatar.png'); // 設定預設圖片
use Laravel\Nova\Fields\Badge; Badge::make('Status') ->map([ 'pending' => 'warning', // 黃色 'approved' => 'success', // 綠色 'rejected' => 'danger', // 紅色 ]);
pending
approved
rejected
use Laravel\Nova\Fields\Boolean; Boolean::make('Active'); // 對應 `active` 欄位 Boolean::make('Published') ->trueValue('yes') // 勾選時儲存為 'yes' ->falseValue('no'); // 未勾選時儲存為 'no'
Laravel Nova 基本操作
欄位 (Fields)
顯示/隱藏欄位控制
Text::make('Name')->showOnIndex(); // 顯示在 Index 頁面 Text::make('Name')->showOnDetail(); // 顯示在 Detail 頁面 Text::make('Name')->showOnCreating(); // 顯示在 Create 表單 Text::make('Name')->showOnUpdating(); // 顯示在 Update 表單 Text::make('Description')->hideFromIndex(); // 隱藏於 Index Text::make('Secret Code')->hideFromDetail(); // 隱藏於 Detail Text::make('Created At')->hideWhenCreating(); // 隱藏於 Create Text::make('ID')->hideWhenUpdating(); // 隱藏於 Update Text::make('Order Status')->onlyOnIndex(); // 僅顯示於 Index Text::make('API Key')->onlyOnDetail(); // 僅顯示於 Detail Text::make('Password')->onlyOnForms(); // 僅顯示於 Create / Update 表單 Text::make('ID')->exceptOnForms(); // 除表單外都顯示顯示/隱藏支援回呼函式(動態控制)
Text::make('Name')->showOnIndex(function () { return $this->name === 'Taylor Otwell'; }); Text::make('Name')->hideFromIndex(function () { return $this->name === 'Taylor Otwell'; });預設值設定
BelongsTo::make('User')->default($request->user()->getKey()); Text::make('Uuid')->default(function ($request) { return Str::orderedUuid(); });欄位佔位符 (Placeholder)
Text::make('Name')->placeholder('請輸入名稱');詳細資料分面板顯示
use Laravel\Nova\Panel; public function fields(Request $request) { return [ ID::make()->sortable(), new Panel('Address Information', $this->addressFields()), ]; } protected function addressFields() { return [ Place::make('Address', 'address_line_1')->hideFromIndex(), Text::make('Address Line 2')->hideFromIndex(), Text::make('City')->hideFromIndex(), Text::make('State')->hideFromIndex(), Text::make('Postal Code')->hideFromIndex(), Country::make('Country')->hideFromIndex(), ]; }限制面板中欄位顯示數量
(new Panel('Profile', [ Text::make('Full Name'), Date::make('Date of Birth'), Text::make('Place of Birth'), ]))->limit(1);欄位類型介紹
Avatar:頭像或圖片上傳欄位
use Laravel\Nova\Fields\Avatar; Avatar::make('Profile Picture', 'profile_picture') ->disk('public') // 儲存在 storage/app/public ->path('avatars') // 存放於 avatars 資料夾 ->prunable() // 刪除資料時一併刪除圖片 ->maxWidth(250) // 限制圖片最大寬度 ->acceptedTypes('image/jpeg,image/png') // 限制可上傳格式 ->default('/images/default-avatar.png'); // 設定預設圖片Badge:狀態標籤顯示欄位
use Laravel\Nova\Fields\Badge; Badge::make('Status') ->map([ 'pending' => 'warning', // 黃色 'approved' => 'success', // 綠色 'rejected' => 'danger', // 紅色 ]);pendingapprovedrejectedBoolean:布林值操作欄位
use Laravel\Nova\Fields\Boolean; Boolean::make('Active'); // 對應 `active` 欄位 Boolean::make('Published') ->trueValue('yes') // 勾選時儲存為 'yes' ->falseValue('no'); // 未勾選時儲存為 'no'