[laravel] Eloquent get only one column as an array

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
}

This question is related to laravel eloquent laravel-5.2

The answer is


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();

I think you can achieve it by using the below code

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


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


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');

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to eloquent

Eloquent: find() and where() usage laravel How to select specific columns in laravel eloquent Laravel Eloquent where field is X or null Laravel Eloquent limit and offset laravel collection to array Eloquent get only one column as an array Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0 Laravel 5.2 - pluck() method returns array Eloquent ORM laravel 5 Get Array of ids eloquent laravel: How to get a row count from a ->get()

Examples related to laravel-5.2

How to uninstall an older PHP version from centOS7 Extension gd is missing from your system - laravel composer Update How to fix error Base table or view not found: 1146 Table laravel relationship table? Setting selected option in laravel form How to clear Route Caching on server: Laravel 5.2.37 Where are logs located? Laravel 5.2 redirect back with success message getting error while updating Composer How to validate phone number in laravel 5.2? Laravel check if collection is empty