duanjun7801 2016-12-21 14:41
浏览 54
已采纳

我应该在laravel中使用EAV模型吗?

I am building a app and I have in my app around 20 categories and each category has some custom fields. So a user picks a category and fills in the custom fields. After that I need to store the values in DB. Can you suggest what would be the best approach to do it. Also I need to be able to filter on this custom fields - like a advanced filter and a performance is a key too.

Should I just make for each category a separate table with their custom fields in such case?

Thank you for help

Dany

  • 写回答

2条回答 默认 最新

  • douchu4048 2016-12-21 15:07
    关注

    There are two options that I can think of.

    1) Create a JSON field called something like attributes in your category table. And then store basically an array of Key Values in it. That will present some challenges when querying on attributes though. I know there are ways around it but i've never needed it so I do not know.

    2) Create a Category Attributes table in your DB that goes something like this

    cat_id - int
    key - varchar
    value - varchar
    Composite Index Unique on [cat_id, key,value ]
    

    Then create a Category Attribute model in laravel and define a hasmany relationship where Category has many Category Attributes

    then querying your categories would go something like this

    $categories = Category::whereHas('CategoryAttributes', function ($query) { $query->where('key', '=', 'color'); $query->where('value','=', 'blue'); })->get();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?