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 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...
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
- How to Get Columns Names from Model in Laravel?
- How to Create Model in Laravel using Command?
- Laravel Ajax GET Request Example Tutorial
- How to Create Custom Middleware in Laravel?
- Laravel 9 Eloquent Mutators and Accessors Example
- How to Get Single Row from Database in Laravel?
- Laravel Eloquent without() and withOnly() Method Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Eloquent Group By Example
- Laravel Eloquent Delete Record By ID Example
- Laravel Eloquent take() and skip() Query Example
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial