Site Admin
Site Admin Founder of MeaningArticles
306 Views

Laravel Eloquent Select Single Column From Array

Hello Dev.

this article will offer some of the most important instance laravel eloquent select single column to array. you can see laravel get particular columns from collection. you can see laravel get simplest one column value. I’m going to reveal you about how to get one column from database in laravel eloquent. permit's get started with laravel get most effective one column value.

you can see bellow instance a way to get one column value in array the use of laravel eloquent method get and pluck. you could 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\Fruit;
  
class ITSController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $fruits = Fruit::pluck('name')->toArray();
          
        dd($fruits);
    }
}

Output

Array
(

    [0] => Apple

    [1] => Banana

    [2] => Grapes

    [3] => Guava
)


Example 2

Controller Code

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Support\Facades\Http;
use App\Models\Fruit;
  
class ITSController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $fruits = Fruits::select('name')->get()->toArray();
          
        dd($fruits);
    }
}

Output

Array
(
    [0] => Array
        (
            [name] => Apple
        )
    [1] => Array
        (
            [name] => Banana
        )
    [2] => Array
        (
            [name] => Grapes
        )
    [3] => Array
        (
            [name] => Guava
        )
)

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.