Laravel 5 QueryBuilder, results in simplified collection

2

I'm using the get() method of Laravel's QueryBuilder and would like to know if it's possible to return the id's in a simple one-dimensional array, for example:

[35,45]

Instead of:

[{"id":35},{"id":45}]
    
asked by anonymous 15.07.2016 / 15:17

2 answers

3

In this case you want a list of id's. It would look something like this:

$ids= DB::table('minha_tabela')->pluck('id');

You can see this at documentation here.

I hope I have helped.

    
15.07.2016 / 16:09
1

QueryBuilder is a framework that has an interface for building sql queries and its main feature is the abstraction of database platforms. I think it does not have tools for manipulating collections, but you can solve your problem by mapping the collection.

Using PHP's PHP function, pass a Closure followed by the collection you want to map by implementing the return of the desired attribute.

Example:

$listaIds = array_map(function($obj){
                return $obj->id;
            }, $listaObjetos);
    
15.07.2016 / 15:56