John Evans Solachuk
John Evans Solachuk

Reputation: 2085

Laravel 4 : How to return an array of elements instead of array of objects from Eloquent?

Right now, I'm doing this :

  $raw_messages_id = Messages::select('id')->get();
  $messages_id = array();
  foreach($raw_messages_id as $message_id){
       array_push($messages_id,$message_id->id);
  }

In order to get this :

[1034,2031,1023,2234,...]

Is there a better approach to this? I want to prevent the use of looping server-side because it takes a lot of time.


What I have tried

$raw_messages_id = Messages::select('id')->get()->toArray();

OR

$raw_messages_id = Messages::select('id')->get(array('id'))->toArray();

unwanted result

[{id:1034},{id:2031},{id:1023},...]

Upvotes: 1

Views: 55

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You may try this:

$ids = Messages::lists('id');

Upvotes: 1

Related Questions