Home Laravel How to Select Specific Columns in Laravel Eloquent Model?

How to Select Specific Columns in Laravel Eloquent Model?

by Shahriar Sagor

Hello dev’s today now in this article i will focused on laravel select the specific columns. From here you’ll learn about how to select specific columns in laravel eloquent. Now this example i will help you about how to select the multiple columns in laravel application. So in this article i will give you a very simple example of laravel get the specific columns from the model. Just need to follow the bellow step for know how to get specific column in laravel eloquent.

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

Let’s start and see the below examples with output:

Example 1: Laravel Select Specific Columns using get()

Here i will use the get() method with the array column names to get specific columns from the database. Now see controller code.

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());
    }
}

Example 2: Laravel Select Specific Columns using first()

Here i will use the first() method also with array column names to get the specific columns from the database. Now see the controller code .

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", "Srsagor")->first(["id", "title", "body"]);
  
        dd($post->toArray());
    }
}

Example 3: Laravel Select Specific Columns using pluck()

Here i will use the pluck() method with the comma-separated column names to get the specific columns from the database. Just see controller code.

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());
    }
}

Example 4: Laravel Select Specific Columns using select()

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

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());
    }

Example 5: Laravel Select Specific Columns using find()

At last i will be use the find() method also with array column names to get the specific columns from the database. Just see controller code and use on your application.

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(6, ["id", "title", "body"]);
  
        dd($post->toArray());
    }
}

Read Also: How to Create Pretty URL Pagination In Laravel ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like