Inside this article we will see the concept i.e Laravel 9 How to Select Specific Columns in Eloquent Model. Article contains the classified information about selecting specific columns in eloquent model in laravel application.
Article is not application version specific, you can use this concept with your version as well. This concept is useful when you want to select few specific columns from database table. Instead of selecting few columns from all table columns, query will be more optimized as well as it will be more efficient to execute to run.
Laravel provides several methods to select data from database table like Using select(), Using get(), Using find(), etc. Here, we will cover all cases to select specific columns.
We will consider a posts table for this article in which we have the columns as id, title, body, category, status.
Learn More –
- Laravel 9 How To Generate UUID in Application Tutorial
- PHP How To Work with Cookie Set Get And Delete Tutorial
- Laravel 9 How To Get All Models From Application Tutorial
- PHP How To Work with Data Pagination Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Database & Connect
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE laravel_app;
To connect database with application, Open .env file from application root. Search for DB_ and update your details.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Method #1: Select Specific Columns using select()
We will use select() eloquent method.
Open any controller which is interacting with the database table then you can use like this –
<?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
Query will select only id, title, body from table.
^ 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"
]
]
Method #2: Select Specific Columns using get()
We will use the get() method with array column names to get specific columns from the database.
<?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
Query will select only id, title, body from table.
^ 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"
]
]
Method #3: Select Specific Columns using find()
We will use the find() method with array column names to get specific columns from the database.
<?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
Query will select only id, title, body from table.
array:3 [▼
"id" => 40
"title" => "Post title 1"
"body" => "Post body"
]
Method #4: Select Specific Columns using first()
We will use the first() method with array column names to get specific columns from the database.
<?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("title", "Post title 1")->first(["id", "title", "body"]); dd($post->toArray()); } }
Output
Query will select only id, title, body from table.
array:3 [▼
"id" => 40
"title" => "Post title 1"
"body" => "Post body"
]
Method #5: Select Specific Columns using pluck()
We will use the pluck() method with comma-separated column names to get specific columns from the database.
<?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
Query will select only id, title from table.
array:5 [▼
40 => "Post title 1"
39 => "Post title 2"
38 => "Post title 3"
37 => "Post title 4"
36 => "Post title 5"
]
We hope this article helped you to learn Laravel 9 How to Select Specific Columns in Eloquent Model Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.