2017-01-17 28 views
0

我有db表类别与列OctoberCMS DB查询和JSON

id name types active 
  • ID - INT,自动增加
  • 名称 - VARCHAR
  • 类型 - JSONABLE
  • ACTIVE - INT

数据库记录考试PLE

1 | Test Category | ["1","2"] | 1 
2 | Another One | ["1","2","3"] | 1 

现在我想创建查询以获得类是有效= 1和类型是阵列(从类型列)。

例如我喜欢的类型是和查询应该看看,如果存在[ “1”, “2”, “3”]

$categories = Db::table('categories') 
    ->where('types', '3') <- ?? 
    ->where('active', 1) 
    ->get(); 

我该怎么办呢?

+0

您正在寻找['FIND_IN_SET()'](http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set)。不知道OctoberCMS,但我的猜测是它会类似于http://stackoverflow.com/questions/35594450/find-in-set-in-laravel-example – Sean

回答

0

我再次回答我自己的问题。经过很少的尝试解决它在组件我只是使部分使用Twig函数的工作。

因此,答案是:

查询在局部

{% for category in categories %} 
    {% if type in category.types %} 
     {{ category.name }} 
    {% endif %} 
{% endfor %} 

$categories = Category::where('active', 1)->get(); 

函数现在它完美地仅示出了类别,其中类型(在这种情况下 )是在jsonable列(根据我的解释国家)。