ItSolutionStuff.com

Laravel Eloquent Select Single Column to Array Example

By Hardik Savani • April 16, 2024
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, laravel 8, laravel 9, laravel 10 and laravel 11 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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Laravel Eloquent When Condition Example

Read Now →
ā˜…

Laravel Eloquent Model Custom Function Example

Read Now →
ā˜…

Laravel Eloquent withSum() and withCount() Example

Read Now →
ā˜…

Laravel Eloquent updateOrCreate Example

Read Now →
ā˜…

Laravel Eloquent firstOrCreate Example

Read Now →
ā˜…

Laravel Eloquent Group By Example

Read Now →
ā˜…

Laravel Eloquent Delete Record By ID Example

Read Now →
ā˜…

Delete All Records from Table in Laravel Eloquent

Read Now →
ā˜…

Laravel Eloquent whereNotNull() Query Example

Read Now →
ā˜…

Laravel Eloquent whereNull() Query Example

Read Now →
ā˜…

Laravel One to Many Eloquent Relationship Tutorial

Read Now →