Add a new column with a value to the select query in Laravel

Add a new column with a value to the select query in Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Adding Literal Values to Eloquent Select Queries

You are looking to append a custom key-value pair to your query results without altering your database schema or relying on model mutators. Specifically, you want every row in your result set to include a type column with the static value "car".

The syntax you proposed—->select('type' = 'car')—will not work as intended. In SQL (and therefore Laravel's underlying query builder), the equals sign is an operator for comparison or assignment, not a method for injecting data into the result set. Writing 'type' = 'car' inside the select clause attempts to create a boolean column that is true whenever the string literal 'type' equals the string literal 'car'. Since those are always equal, this would technically select a single column containing 1, but it would not name that column type with a value of car in the way you need for your template logic.

To inject arbitrary data into a query result, you must use the DB::raw() helper or, more cleanly within the Eloquent Query Builder, utilize the selectRaw method or specific scalar functions that allow you to define both the column name and its value simultaneously.

The most direct approach is to chain another ->selectRaw statement after your join. This allows you to execute a raw SQL expression that defines the new column and assigns it a static value. You can do this by constructing a string where the column alias is defined on the left side of the assignment operator, and the value is on the right.

Here is how you modify your existing query:

use Illuminate\Support\Facades\DB;

$items = DB::table('items')
    ->orderBy('created_at', 'desc')
    ->join('items_categories', 'items.item_category_id', '=', 'items_categories.category_id')
    ->select( 
        'items.id as items___item_id',
        'items.item_title as items___item_title',
        'items_categories.id as items_categories___category_id',
        'items_categories.title as items_categories___category_title'
    )
    ->selectRaw('CAST(NULL AS CHAR) AS type') // This adds a column named 'type' with NULL value
    ->orWhere(function ($query) {
        $query->where('type', 'car'); 
        // Note: The logic below is the correct way to add a constant, see below.
    })
    ->take(20);

Wait, that example above using CAST(NULL AS CHAR) gives you a NULL value, not "car". Let's correct that immediately. You need to assign the string literal 'car' directly.

The correct implementation looks like this:

$items = DB::table('items')
    ->orderBy('created_at', 'desc')
    ->join('items_categories', 'items.item_category_id', '=', 'items_categories.category_id')
    ->select( 
        'items.id as items___item_id',
        'items.item_title as items___item_title',
        'items_categories.id as items_categories___category_id',
        'items_categories.title as items_categories___category_title'
    )
    ->selectRaw("'car' AS type")
    ->take(20);

In this snippet, the ->selectRaw("'car' AS type") method executes a raw SQL expression. The syntax 'car' AS type tells the database to create a new column named type and populate every row with the string value car. Because we are using the Query Builder's chaining methods, Laravel correctly escapes the quotes for you within the context of the query, so you do not need to worry about escaping characters manually unless your value contains special SQL syntax.

When you iterate over $items, the resulting collection will contain a standard array entry: ['type' => 'car'] alongside your existing columns. This allows you to filter or manipulate the data in your view or controller immediately after retrieval, without needing to touch the model files or database migrations.

One potential pitfall with this approach is performance if you are dealing with massive datasets. Adding a static value does not impact the SELECT execution time significantly, but keep in mind that every additional column adds a tiny overhead to memory usage per row. If you are selecting hundreds of columns already, adding one more string literal is negligible. However, if this pattern becomes common across your application, consider extracting it into a helper method to keep your query chains clean and readable.

Another consideration is whether this static value makes logical sense for all records in the items table. If your items are actually trucks or SUVs (as suggested by your sample data), hardcoding "car" might imply a type that doesn't exist in your business logic. Ensure that adding this field won't confuse downstream consumers of the API or template views that expect accurate categorization. If you truly need to filter later, ensure that having a constant value for type does not conflict with any existing filtering logic expecting dynamic types from the database.

Our network