How addSelect() Builder method works in Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Understanding addSelect() Method for Eloquent Queries in Laravel Body: The addSelect() method in Laravel is an essential tool for developers to customize their database queries and retrieve additional columns without modifying the model's definition. This blog post aims to provide a comprehensive understanding of how this builder method works, along with best practices and workarounds related to it. Firstly, let's understand how addSelect() is utilized in Laravel. Assume we have the models as follows: 1) User Model:
namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'User';

    protected $fillable = [
        'username', 'favoriteColor'
    ];

    public function flights()
    {
        return $this->hasMany('App\Flight');
    }
}
2) Flight Model:
namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'Flight';

    protected $fillable = [
        'flightName'
    ];
}
We can perform eager loading to fetch user data along with their flights by running the following query:
$usersWithFlights = User::with(['flights'])->get();
Output: [{ "userID": 1, "username": "John", "favoriteColor": "Red", "flights": [ { "flightID": 2240, "flightName": "To Beijing fellas" }, { "flightID": 4432, "flightName": "To Russia fellas" } ] }, ...] Now, let's see how to add a 'number' column using addSelect() in the Eloquent query.
$usersWithFlights = User::with(['flights'])->addSelect(DB::raw('1 as number'))->get();
The output will be: [{ "userID": 1, "username": "John", "favoriteColor": "Red", "number": 1, "flights": [] }, ...] Since the flights array is empty in this case, it means that we're using Eloquent relationships with lazy loading. In such scenarios, relations are not loaded until required explicitly or inside loops. To load the related objects eagerly, you can use has() instead of hasMany() or change your relationship to belongsToMany(). However, addSelect() is mainly used for adding custom columns based on database queries. For example:
$usersWithFlights = User::with(['flights'])->addSelect('COUNT(*) as total_flights')->get();
This query will count the number of flights related to each user and retrieve their data along with the flights. For more complex use cases, you can try the withQuery() method that lets you modify the raw SQL queries:
$usersWithFlights = User::query()->selectRaw('*, (SELECT count(*) FROM Flights where UserID=User.userID) as total_flights')->get();
This query will get user data along with their total number of flights in the output. If there's no need to modify the database queries, you may use select() instead of addSelect(). In conclusion, understanding how and when to use these methods is crucial for building efficient Laravel applications. Happy coding!