在Laravel/PHP/MySQL中保存和显示带有子页面的页面所需的数据库逻辑建议

在Laravel/PHP/MySQL中保存和显示带有子页面的页面所需的数据库逻辑建议,php,mysql,laravel-4,logic,eloquent,Php,Mysql,Laravel 4,Logic,Eloquent,这是我正在构建的第一个应用程序(在Laravel中),它不是教程中的“待办事项”列表,我正在寻找实现某些数据库逻辑的最佳方法 我正在构建一个应用程序,用户可以在其中创建页面和子页面。但是,有不同类型的页面模板,例如: 音频:(用于嵌入Soundcloud) 视频:(用于嵌入YouTube) 每个模板可以与自定义名称和slug一起使用多次,这意味着您可以有多个标题为“曲目”或“混音”的音频模板 前端URL结构如下所示: website.com/username/mixes/-显示混音列表 we

这是我正在构建的第一个应用程序(在Laravel中),它不是教程中的“待办事项”列表,我正在寻找实现某些数据库逻辑的最佳方法

我正在构建一个应用程序,用户可以在其中创建页面和子页面。但是,有不同类型的页面模板,例如:

  • 音频:(用于嵌入Soundcloud)
  • 视频:(用于嵌入YouTube)
每个模板可以与自定义名称和slug一起使用多次,这意味着您可以有多个标题为“曲目”或“混音”的音频模板

前端URL结构如下所示:

website.com/username/mixes/-显示混音列表

website.com/username/mixes/name-of-mix-直接转到每个混音

在管理区域中,用户将遵循以下路径:

  • 添加新页面(简单链接)

  • 选择模板(如音频或视频)

  • 选择音频模板并将其命名为混音(前端 网址:website.com/username/mixes)

  • 返回到新创建的(但为空)混合页面

  • 用户单击“添加新音频项”

  • 创建名为“混合名称”的新音频项

  • 这将迫使段塞的前一部分被/混合/和 然后在末尾添加“混合名称”

  • 用户可以在Mixes页面重复多次

  • 我可以实现以上所有功能(在Laravel的控制器中)

    我遇到的问题是,在数据库中实现这一点的最佳/最干净的方法是什么

    我是不是

    选项一-------------------

    保存页面模型中的每个页面和子页面,并从中引用模板(如果是子页面),例如:

    登录页(website.com/username/mixes)

    新页面:

    - id: 1
    - user_id: 1
    - format: audio
    - format_id: null
    - template: audio_landing
    - title: Mixes
    - slug: mixes
    
    - id: 2
    - user_id: 1
    - format: audio
    - format_id: 6
    - template: audio_single
    - title: null (title is on the audio model)
    - slug: mixes/name-of-mix
    
    - id: 1
    - user_id: 1
    - template: audio_landing
    - title: Mixes
    - slug: mixes
    
    单页(website.com/username/mixes/name of mix)

    保存后,它将创建一个页面,还将创建一个单独的音频项

    新音频:

    - id: 6
    - user_id: 1
    - title: Name of mix
    - embed_code: 7654365 
    
    - id: 6
    - user_id: 1
    - template: audio_single
    - title: Name of mix
    - embed_code: 7654365 
    - slug: mixes/name-of-mix
    
    新页面:

    - id: 1
    - user_id: 1
    - format: audio
    - format_id: null
    - template: audio_landing
    - title: Mixes
    - slug: mixes
    
    - id: 2
    - user_id: 1
    - format: audio
    - format_id: 6
    - template: audio_single
    - title: null (title is on the audio model)
    - slug: mixes/name-of-mix
    
    - id: 1
    - user_id: 1
    - template: audio_landing
    - title: Mixes
    - slug: mixes
    
    然后在我看来,我会调用website.com/
    {username}
    /
    {slug}

    查询将只扫描用户模型(用户名),然后是页面模型,在找到正确的slug时停止,然后引用关联的音频模型并为该页面创建视图。这(我认为)将是一种更快、强度更低(在MySQL上)的获取页面信息的方式

    我的想法正确吗?从页面模型中,我可以使用“
    格式:音频”
    ”搜索音频模型,并使用“
    格式\u id:6
    ”将其与模型匹配“
    id:6

    方案二-------

    在页面中保存页面,然后在音频模型中保存子页面

    登录页(website.com/username/mixes)

    新页面:

    - id: 1
    - user_id: 1
    - format: audio
    - format_id: null
    - template: audio_landing
    - title: Mixes
    - slug: mixes
    
    - id: 2
    - user_id: 1
    - format: audio
    - format_id: 6
    - template: audio_single
    - title: null (title is on the audio model)
    - slug: mixes/name-of-mix
    
    - id: 1
    - user_id: 1
    - template: audio_landing
    - title: Mixes
    - slug: mixes
    
    单页(website.com/username/mixes/name of mix)

    这将直接保存在音频模型中

    新音频:

    - id: 6
    - user_id: 1
    - title: Name of mix
    - embed_code: 7654365 
    
    - id: 6
    - user_id: 1
    - template: audio_single
    - title: Name of mix
    - embed_code: 7654365 
    - slug: mixes/name-of-mix
    
    然后在我看来,我会调用website.com/
    {username}
    /
    {slug}

    缺点是(我认为)在找到正确的slug之前,查询必须搜索页面、音频、视频、演唱会、博客、事件等模型。我想,每次加载页面请求时,这会对数据库造成很大的影响

    对不起,我要说一点,欢迎您提供任何建议


    杰克。

    您是否考虑过只为slug设置一个表,从而为应用程序的路由逻辑提供一个单独的位置来确定它必须显示什么


    slugs表当然需要一个“type”字段来标识它所指向的对象的类型。

    是的,这很有意义,我将尝试一下:)