首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用elequent生成器查询Laravel模型中的Json属性字段

用elequent生成器查询Laravel模型中的Json属性字段
EN

Stack Overflow用户
提问于 2017-10-29 09:23:13
回答 2查看 2.5K关注 0票数 2

我想要过滤一个json字段(post)在数据库中与自序建设者。

我需要帮助来询问顺序。

代码语言:javascript
运行
复制
attributes: array:17 [▼
    "id" => 2
    "admin_id" => 0
    "email" => "test@gmail.com"
    "fullname" => "asdasdasd"
    "subject" => "asdasdasdasd"
    "desc" => """
      sadasd\r\n
      """
    "cell_phone" => "091200000000"
    **"post" => "{"modir": 0, "saken": 0, "ozv_modir": 0}"**
    "elevator_spec" => null
    "type" => null
    "control" => null
    "count_stop" => null
    "door_type" => null
    "lift_capacity" => null
    "insurance" => null
    "created_at" => "2017-10-28 21:24:54"
    "updated_at" => "2017-10-28 21:24:54"
  ]
EN

Stack Overflow用户

回答已采纳

发布于 2017-10-29 09:29:04

代码语言:javascript
运行
复制
You should use setter and getter in the model in case of that specific field that you want .

     public function setPostAttribute($value)
    {
        $this->attributes['post'] = json_encode($value, JSON_UNESCAPED_UNICODE);
    }

   public function getPostAttribute()
    {
        $model = new $this;
        $jsonToConvert = $this->attributes['post'];
        $modelArray = $model->fromJson($jsonToConvert);

        return $model->newInstance($modelArray);
    }


after that in your controller :

    Orders::where('post->modir', 0)->get(
票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46998615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档