Laravel Eloquent Select Single Column to Array Example

By Hardik Savani July 19, 2021 Category : Laravel

This article will provide some of the most important example laravel eloquent select single column to array. you can see laravel get specific columns from collection. you can see laravel get only one column value. I’m going to show you about how to get one column from database in laravel eloquent. Let's get started with laravel get only one column value.

You can see bellow example how to get one column value in array using laravel eloquent method get and pluck. you can use this example with laravel 6, laravel 7 and laravel 8 version.

Example 1:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

use App\Models\Product;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$products = Product::pluck('name')->toArray();

dd($products);

}

}

Output:

Array

(

[0] => Samsung Galaxy

[1] => Apple iPhone 12

[2] => Google Pixel 2 XL

[3] => LG V10 H800

)

Example 2:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

use App\Models\Product;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$products = Product::select('name')->get()->toArray();

dd($products);

}

}

Output:

Array

(

[0] => Array

(

[name] => Samsung Galaxy

)

[1] => Array

(

[name] => Apple iPhone 12

)

[2] => Array

(

[name] => Google Pixel 2 XL

)

[3] => Array

(

[name] => LG V10 H800

)

)

i hope it can help you...