140

How to get only one column as one dimentional array in laravel 5.2 using eloquent?

I have tried:

$array = Word_relation::select('word_two')->where('word_one', $word_id)->get()->toArray();

but this one gives it as 2 dimentional array like:

array(2) {
      [0]=>
      array(1) {
        ["word_one"]=>
        int(2)
      }
      [1]=>
      array(1) {
        ["word_one"]=>
        int(3)
      }
    }

but I want to get it as:

array(2) {
    [0]=>2
    [1]=>3
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Riiwo
  • 1,839
  • 3
  • 15
  • 18

5 Answers5

274

You can use the pluck method:

Word_relation::where('word_one', $word_id)->pluck('word_two')->toArray();

For more info on what methods are available for using with collection, you can you can check out the Laravel Documentation.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Thanks, that was the function I saw somewhere but didn't find it anymore, also for the record, for the answer to be presice, then maybe add ->toArray() since it returns colletion at the moment and select can be left out from the query for laravel 5.2 at least. – Riiwo Jan 20 '16 at 23:10
  • True about the `select` being redundant, but I don't see a problem with having a collection as a result, because a collection is just a _fancy_ array that can be iterated over the same as an array. I rarely use arrays instead of collections since the memory footprint is generally not a problem, and collections can be easily cast to arrays where needed because they implement the [`toArray`](https://github.com/laravel/framework/blob/5.2/src/Illuminate/Support/Collection.php#L961-L967) method. However, for consistency with your question, I modified the answer to do convert the result. – Bogdan Jan 20 '16 at 23:19
  • At the moment I am quite new to laravel and eloquent, so I needed these as array of numbers to later merge the array with similar one and use it in another query – Riiwo Jan 20 '16 at 23:50
  • Thank you for sharing this! – Bhargav Nanekalva Aug 10 '16 at 06:58
  • 4
    While a `Collection` also has a `pluck()` method - What you are using is a method of [`QueryBuilder`](https://laravel.com/docs/5.4/queries#retrieving-results). – Paul Spiegel Feb 14 '17 at 09:56
  • why toArray()? only pluck is good enough i think. Oh!! got your point. sorry for pointing this. – Avnish Tiwary Aug 26 '18 at 17:51
16

If you receive multiple entries the correct method is called lists.

    Word_relation::select('word_two')->where('word_one', $word_id)->lists('word_one')->toArray();
moxx
  • 189
  • 1
  • 6
15

That can be done in short as:

Model::pluck('column')

where model is the Model such as User model & column as column name like id

if you do

User::pluck('id') // [1,2,3, ...]

& of course you can have any other clauses like where clause before pluck

Md. A. Apu
  • 769
  • 10
  • 30
8

I came across this question and thought I would clarify that the lists() method of a eloquent builder object was depreciated in Laravel 5.2 and replaced with pluck().

// <= Laravel 5.1
Word_relation::where('word_one', $word_id)->lists('word_one')->toArray();
// >= Laravel 5.2
Word_relation::where('word_one', $word_id)->pluck('word_one')->toArray();

These methods can also be called on a Collection for example

// <= Laravel 5.1
  $collection = Word_relation::where('word_one', $word_id)->get();
  $array = $collection->lists('word_one');

// >= Laravel 5.2
  $collection = Word_relation::where('word_one', $word_id)->get();
  $array = $collection->pluck('word_one');
SamBremner
  • 793
  • 2
  • 10
  • 22
-4

I think you can achieve it by using the below code

Model::get(['ColumnName'])->toArray();

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
saeid
  • 37
  • 5
  • Can you please explain that. – J...S Oct 17 '17 at 19:28
  • 1
    As far as I am aware doing `Model::get(['ColumnName'])->toArray();` is equivalent of doing `Model::select('ColumnName')->get()->toArray()` which results in a multi dimensional array. – SamBremner Jan 05 '18 at 11:43