dongqing904999 2015-09-26 17:35
浏览 39

在Laravel 5中创建具有多个子值的Model /表的正确方法

As per the comments, I've reworked this question I'm new to Laravel 5 and MVC in general, so my question is aimed more at the proper "MVC" way of doing this.

I've got a Model/table (Category/categories), that have name, slug, and id columns. This would be things like "Brand", "Size", "Color". I then need to have multiple values for each of these. For example, "Size" would have "Small", "Medium", and "Large".

I would create a table with columns something like: id, category_id, name/value to accomplish this, and then do a join on the categories id value.

In Laravel however, I could create a CategoryValue model which would have a belongsTo() for the Category model, and vice versa. This seems like overkill for something that is simply setting/getting list values. Is this the correct Laravel/MVC way of doing it?

On that same note, my Item Model will have to store its values for each Category. I need to link the Item to the Category and the CategoryValue. At this level, do I create two separate pivot tables? Or do I have one pivot table with three values, such that it would have item_id, category_id, categoryValue_id? I don't want to create excess models and tables.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 远程桌面文档内容复制粘贴,格式会变化
    • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题