How to Select Specific Columns in Laravel Eloquent Model?

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 AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsPost;

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..medrectangle-4-multi-155{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsPost;

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 AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsPost;

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 AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsPost;

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 AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsPost;

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…

1 thought on “How to Select Specific Columns in Laravel Eloquent Model?”

Leave a Comment