简体   繁体   中英

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},...]

您可以尝试以下方法:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM