How to Select Specific Columns in Laravel Eloquent Model?

By Hardik Savani November 5, 2023 Category : Laravel

Hi Dev,

This article is focused on laravel select specific columns. you'll learn how to select specific columns in laravel eloquent. this example will help you how to select multiple columns in laravel. This article will give you a simple example of laravel get specific columns from model. follow bellow step for how to get specific column in laravel eloquent.

If you need to get or select specific columns from the eloquent model then laravel provides several ways to get specific fields from the database. laravel also provides an eloquent method for its documentation, but here, I will give you all methods one by one with output so you can use it whatever you need.

Let's see the below examples with output:

Example 1: Laravel Select Specific Columns using select()

we will use the select() method with comma-separated column names to get specific columns from the database. see controller code and output.

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::select("id", "title", "body")

->latest()

->get();

dd($posts->toArray());

}

}

Output:

^ array:5 [â–¼

0 => array:3 [â–¼

"id" => 40

"title" => "Post title 1"

"body" => "Post body"

]

1 => array:3 [â–¼

"id" => 39

"title" => "Post title 2"

"body" => "Post body"

]

2 => array:3 [â–¼

"id" => 38

"title" => "Post title 3"

"body" => "Post body"

]

]

Example 2: Laravel Select Specific Columns using get()

we will use the get() method with array column names to get specific columns from the database. see controller code and output.

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::latest()

->get(["id", "title", "body"]);

dd($posts->toArray());

}

}

Output:

^ array:5 [â–¼

0 => array:3 [â–¼

"id" => 40

"title" => "Post title 1"

"body" => "Post body"

]

1 => array:3 [â–¼

"id" => 39

"title" => "Post title 2"

"body" => "Post body"

]

2 => array:3 [â–¼

"id" => 38

"title" => "Post title 3"

"body" => "Post body"

]

]

Example 3: Laravel Select Specific Columns using find()

we will use the find() method with array column names to get specific columns from the database. see controller code and output.

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$post = Post::find(40, ["id", "title", "body"]);

dd($post->toArray());

}

}

Output:

array:3 [â–¼

"id" => 40

"title" => "Post title 1"

"body" => "Post body"

]

Example 4: Laravel Select Specific Columns using first()

we will use the first() method with array column names to get specific columns from the database. see controller code and output.

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$post = Post::where("name", "Hardik")->first(["id", "title", "body"]);

dd($post->toArray());

}

}

Output:

array:3 [â–¼

"id" => 40

"title" => "Post title 1"

"body" => "Post body"

]

Example 5: Laravel Select Specific Columns using pluck()

we will use the pluck() method with comma-separated column names to get specific columns from the database. see controller code and output.

PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::latest()

->pluck('title', 'id');

dd($posts->toArray());

}

}

Output:

array:5 [â–¼

40 => "Post title 1"

39 => "Post title 2"

38 => "Post title 3"

37 => "Post title 4"

36 => "Post title 5"

]

I hope it can help you...

Tags :
Shares