Laravel Eloquent Select Single Column to Array Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent When Condition Example
- Laravel Eloquent Model Custom Function Example
- Laravel Eloquent withSum() and withCount() Example
- Laravel Eloquent updateOrCreate Example
- Laravel Eloquent firstOrCreate Example
- Laravel Eloquent Group By Example
- Laravel Eloquent Delete Record By ID Example
- Delete All Records from Table in Laravel Eloquent
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent whereNull() Query Example
- Laravel One to Many Eloquent Relationship Tutorial