Framework: [5.2] Pluck query by json column

Created on 21 Jun 2016  路  10Comments  路  Source: laravel/framework

When I make a query with plucking results by json property, I am getting a collection with nulls. For example:
\App\Entities\User::find(1140)->logins()->pluck('payload->ip')
Expected result: array with ips
Actually result:

Illuminate\Support\Collection {#1326
     all: [
       null,
       null,
       null,
     ],
   }

The results are correct but on a moment of a plucking they break.
laravel/framework/src/Illuminate/Database/Query/Builder.php

Most helpful comment

I've found the trick for this issue.
If I am using pluck('payload->ip AS ip') then everything is okay.

All 10 comments

It's extremely tricky to get this to work with the current way the query builder works :)

I've found the trick for this issue.
If I am using pluck('payload->ip AS ip') then everything is okay.

nice :) Thanks for sharing.

@MCMatters I'm currently in need for similar feature, but after trying both above syntaxes it seems to be not working, can you kindly confirm how you manage to make it work for you? 馃檪

@Omranic, sure. Here is my example http://take.ms/BRQlV

Thanks @MCMatters I see it's working on queries now, that's helpful 馃憤
Have you tried to make it work with normal collections?

@Omranic Yes. Just add casting to the attribute in the model and call pluck like this http://take.ms/KHT1i
I hope it will be useful

Awesome, really appreciated 馃槉
Thanks @MCMatters

I've found the trick for this issue.
If I am using pluck('payload->ip AS ip') then everything is okay.

this works for me on localhost, but throws syntax error on shared hosting

I know it's old, but correct syntax is:

$model->pluck('json.key.key'));

Like:
$books->pluck('details.pages.page');

Was this page helpful?
0 / 5 - 0 ratings