ItSolutionStuff.com

How to Select Specific Columns in Laravel Eloquent Model?

By Hardik Savani • November 5, 2023
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: Laravel
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

How to Get Columns Names from Model in Laravel?

Read Now →

How to Create Model in Laravel using Command?

Read Now →

Laravel Ajax GET Request Example Tutorial

Read Now →

How to Create Custom Middleware in Laravel?

Read Now →

Laravel 9 Eloquent Mutators and Accessors Example

Read Now →

How to Get Single Row from Database in Laravel?

Read Now →

Laravel Eloquent without() and withOnly() Method Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Eloquent Group By Example

Read Now →

Laravel Eloquent Delete Record By ID Example

Read Now →

Laravel Eloquent take() and skip() Query Example

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →