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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?