鍍金池/ 問答/PHP/ 怎么模糊查詢,求解 謝謝

怎么模糊查詢,求解 謝謝

"items": [

    {
        "id": 1,
        "code": "",
        "parent_id": 0,
        "name": "國有經(jīng)濟型",
        "sort": 0,
        "created_at": "1509343337",
        "updated_at": "1510018421",
        "operate_staff_id": 1,
        "operater": {
            "id": 1,
            "username": "admin",
            "created_at": null,
            "updated_at": "1509526903",
            "idcard": null,
            "mobile": null,
            "realname": null,
            "status": 1,
            "operate_staff_id": 3,
            "sort": 0,
            "code": "",
            "sex": "未知"
        },
        
  $this->model = $this->model->where(function ($query) use ($key) {
                $query->orWhere('name', 'like', '%' . $key . '%')
                    ->orWhere('parent_id', 'like', '' . $key . '%')
                    ->orWhere('code', 'like', '' . $key . '%');
            });      
        
        我想請問,這里面如果用模糊查詢  查到我需要的 operater里面的username呢
        添加operater.username  和  operater['username'] 都不行
        
回答
編輯回答
空白格

你這個都不是一個數(shù)組,是Json格式的吧?需先轉(zhuǎn)回php。而且你具體是要做sql模糊查詢還是取值?

2017年8月4日 03:18
編輯回答
萌吟

如果是前臺傳到后臺的json數(shù)據(jù)的話,就要json_decode()解析后,直接讀取就行,你需要拿到username的話,就可以這樣寫:

$arr = json_decode($items);//items為接收到的字符串
$username = $arr['operater']->username;
2018年7月23日 05:33