🔍 搜尋結果:toArray

🔍 搜尋結果:toArray

在 Twill CMS 建立模組之間的關聯

繼續研究比較進階的功能吧 假設客戶是一間設計顧問公司好了 在台北、台中、高雄都有辦公室,存在 offices 模組 然後官網有許多 pages 要展示,每個 page 是由不同的辦公室設計 要如何建立這種關聯呢?又該如何在前台存取關聯模組呢?操作起來就跟 laravel 原生功能一樣嗎? --- 參考官網文件 https://twillcms.com/docs/relations/one-to-many.html ``` php artisan make:migration add_office_id_to_pages_table ``` 把 `office_id` 欄位加好之後,直接進 mysql 把 pages 加上 office_id 試試看! 接著在 Office.php 加入 hasMany 然後在 Page.php 加入 belongsTo 再來試試看在 page.blade.php 加入 ``` <hr /> by office: {{ $item->office->title }} ``` 然後在 office.blade.php 加入 ``` <hr /> pages count: {{ $item->pages->count() }} ``` 打開頁面會發現...成功運作! 也就是關聯操作完全跟 laravel 原生語法一模一樣,真是太棒了 ref commit: https://github.com/howtomakeaturn/twill-play/commit/d5b211a69661a2f456f874e8aac5fd51603fc4a8 --- 最後,不能真的叫客戶開 mysql 進去修改資料。來更新管理面板,給客戶 GUI 使用吧! 這邊一樣文件有點不足,參閱了幾份文件&原始碼 https://twillcms.com/docs/relations/one-to-many.html https://twillcms.com/docs/form-fields/select.html PageController 加入這段 ``` $form->add( Select::make()->name('office_id')->label('Office') ->options(Office::all()->map(function (Office $office) { return [ 'value' => $office->id, 'label' => $office->title, ]; })->toArray()) ); ``` 然後在 App\Models\Page 的 `$fillable` 陣列加入 'office_id' 打開管理面板,大功告成! 這段程式碼修改,依然跟原生 laravel 操作完全相容 實在是強大、開發者友善的 CMS 架構啊! re commit: https://github.com/howtomakeaturn/twill-play/commit/be1365f9a166ff28c12bd6936d7b7968fbdaa3d9