Daxsis's avatar

Model select from json column returns `json_unquote..`

Hello everybody, long time lurker and found some awesome answers, however this time I encountered what seems unusual problem. My setup: Laravel 6.16 Mysql 8.0.19

I have an Activity table and model with json column name and when I try to query it I get this

>>> App\Activity::whereIn('id', [16,25,50])->get(['id', 'name->en'])
=> Illuminate\Database\Eloquent\Collection {#3034
     all: [
       App\Activity {#3035
         id: 16,
         json_unquote(json_extract(`name`, '$."en"')): "8844ge32v",
       },
       App\Activity {#3036
         id: 25,
         json_unquote(json_extract(`name`, '$."en"')): "21972qf55",
       },
       App\Activity {#3037
         id: 50,
         json_unquote(json_extract(`name`, '$."en"')): "700z48306",
       },
     ],
   }
0 likes
10 replies
Sinnbeck's avatar

Can you try this?

App\Activity::whereIn('id', [16,25,50])->get(['id', 'name->>en'])
Daxsis's avatar

Tried, the same. Just in case PHP 7.2.19

>>> App\Experience::whereIn('id', [16,25,50])->get(['id', 'name->>en'])
=> Illuminate\Database\Eloquent\Collection {#3019
     all: [
       App\Experience {#3040
         id: 16,
         json_unquote(json_extract(`name`, '$.">en"')): null,
       },
       App\Experience {#3039
         id: 25,
         json_unquote(json_extract(`name`, '$.">en"')): null,
       },
       App\Experience {#3038
         id: 50,
         json_unquote(json_extract(`name`, '$.">en"')): null,
       },
     ],
   }
Sinnbeck's avatar

Ok try with a select instead.

App\Experience::whereIn('id', [16,25,50])->select('id', 'name->>en as name')->get();
Daxsis's avatar
>>> App\Experience::whereIn('id', [16,25,50])->select('id', 'name->>en as name')->get();
=> Illuminate\Database\Eloquent\Collection {#3028
     all: [
       App\Experience {#3027
         id: 16,
         name: null,
       },
       App\Experience {#3026
         id: 25,
         name: null,
       },
       App\Experience {#3025
         id: 50,
         name: null,
       },
     ],
   }

weird :)

Daxsis's avatar

I'm using MySql 8.0.19 in Laragon, which I suppose has to be compatible with field->key notation :)

This is an example of the data in the table

>>> App\Activity::find(16)
=> App\Activity {#3028
     id: 16,
     name: "{"en": "8844ge32v"}",
     description: "{"en": "Rem necessitatibus aut perferendis commodi laborum ullam ut. Illum cumque itaque vel magnam perspiciatis vel alias."}",
     product: "{"en": "Et eum doloribus facere rerum rerum perferendis itaque. Omnis sequi animi perferendis atque. Est magni tempore eum deleniti eligendi. Quas repellat sit ab reprehenderit nobis aspernatur quis."}",
     updated_by: null,
     created_at: "2020-02-23 21:11:06",
     updated_at: "2020-02-23 21:11:06",
   }
Sinnbeck's avatar

Oh sorry missed that. Can you try the select version with only one arrow?

Daxsis's avatar

Sadly the same

>>> App\Activity::where('id', 1)->get('name->en')
=> Illuminate\Database\Eloquent\Collection {#3031
     all: [
       App\Activity {#3034
         json_unquote(json_extract(`name`, '$."en"')): "46939egi4",
       },
     ],
   }
staudenmeir's avatar
Level 24

You need a single arrow and an alias:

App\Activity::where('id', 1)->get('name->en as name')
3 likes
Daxsis's avatar

It works!

>>> App\Activity::where('id', 1)->get(['id', 'name->en as name'])
=> Illuminate\Database\Eloquent\Collection {#3033
     all: [
       App\Activity{#3027
         id: 1,
         name: "46939egi4",
       },
     ],
   }

Can you explain why native doesn't solve it and I have to use alias?

Please or to participate in this conversation.