🔧 阿川の電商水電行
Shopify 顧問、維護與客製化
💡
小任務 / 單次支援方案
單次處理 Shopify 修正/微調
⭐️
維護方案
每月 Shopify 技術支援 + 小修改 + 諮詢
🚀
專案建置
Shopify 功能導入、培訓 + 分階段交付

繼續研究比較進階的功能吧

假設客戶是一間設計顧問公司好了

在台北、台中、高雄都有辦公室,存在 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


每週二 YT 直播問答,DC 可 Call-in,一起討論技術、職涯發展。
YT 👇👇👇
https://www.youtube.com/@codelove_tw
DC 👇👇👇
https://discord.gg/fvCF2whZ9D

提供 Shopify 接案服務:阿川の電商水電行 - 專修 Shopify 網店 👇👇👇
https://job.turn.tw/shopify-services

共有 0 則留言


每週二 YT 直播問答,DC 可 Call-in,一起討論技術、職涯發展。
YT 👇👇👇
https://www.youtube.com/@codelove_tw
DC 👇👇👇
https://discord.gg/fvCF2whZ9D

提供 Shopify 接案服務:阿川の電商水電行 - 專修 Shopify 網店 👇👇👇
https://job.turn.tw/shopify-services
🏆 本月排行榜
🥇
站長阿川
📝11   💬6   ❤️3
470
🥈
我愛JS
📝3   💬8   ❤️7
166
🥉
AppleLily
📝1   💬4   ❤️1
64
#4
xxuan
💬1  
3
#5
💬1  
3
評分標準:發文×10 + 留言×3 + 獲讚×5 + 點讚×1 + 瀏覽數÷10
本數據每小時更新一次
🔧 阿川の電商水電行
Shopify 顧問、維護與客製化
💡
小任務 / 單次支援方案
單次處理 Shopify 修正/微調
⭐️
維護方案
每月 Shopify 技術支援 + 小修改 + 諮詢
🚀
專案建置
Shopify 功能導入、培訓 + 分階段交付