How to alias the name of a column in Eloquent

How to alias the name of a column in Eloquent

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

Stefan Bogdanescu

Founder & Senior Architect

How to Alias the Name of a Column in Eloquent

When you execute a query on the products table joined with a tags table, Laravel's Eloquent ORM automatically maps database columns to model attributes based on their casing and naming conventions. By default, a column named name in the tags table will appear as name in your resulting array. If you need that attribute to be accessible as tag_name instead, you must explicitly alias the column within your SQL query.

Laravel's relationship magic handles fetching related data, but it does not automatically rename columns during a raw join operation unless you instruct the query builder to do so. To achieve the desired output where $result[$i]['tag_name'] exists, you need to use the DB::raw() function or the specific column aliasing syntax provided by the query builder.

The most direct approach is to modify your scopeJoinWithTags method to include an alias in the join condition or, more commonly, in a subsequent select statement. However, simply adding an alias during the leftJoin itself often only affects how the table is referenced in joins, not necessarily the final column names returned in the result set unless you are careful.

To ensure the attribute appears as tag_name in the final array, you should explicitly select the aliased column. Here is how you can implement this using the query builder syntax:

use Illuminate\Support\Facades\DB;

public function scopeJoinWithTags($query)
{
    return $query->leftJoin('tags', 'tags.id', '=', 'products.tag_id')
        ->select(
            'products.*',
            DB::raw('tags.name as tag_name')
        );
}

In this snippet, the DB::raw function wraps the SQL expression tags.name as tag_name. This tells the database engine to return the column under the new name. When Eloquent hydrates the model or converts the collection to an array using toArray(), it reads these alias names directly from the result set. Consequently, accessing $model->tag_name or iterating over the array with ['tag_name'] becomes possible without needing to manually map keys afterwards.

If you prefer not to use the raw SQL function, you can utilize the query builder's string interpolation which is also supported in recent Laravel versions:

use Illuminate\Database\Query\Builder;

public function scopeJoinWithTags($query)
{
    return $query->leftJoin('tags', 'tags.id', '=', 'products.tag_id')
        ->select(['products.*', 'tags.name as tag_name']);
}

This approach is cleaner and avoids the DB::raw helper, making the intent more obvious to other developers reading the code. It effectively solves the immediate problem of changing $result[$i]['name'] to $result[$i]['tag_name'].

A critical consideration when aliasing columns in Eloquent involves model relationships. If your Product model has a belongsToMany or belongsTo relationship defined with the tags table, Eloquent might attempt to merge attributes from the related models into the main model's attribute array. If the column is aliased as tag_name, but the relationship expects a standard name attribute for filling, you may encounter discrepancies depending on how you access the data.

For instance, if you are using toArray() without specifying ['only' => ...], Eloquent will expose all columns found in the result set. If an alias exists, it takes precedence over the original column name in the array representation. However, if you rely on $product->tags->name to access data from the joined table via a relationship accessor, Eloquent looks for name specifically. In such cases, simply aliasing the raw join might not be enough; you may need to adjust the relationship mapping or use with() to eager load the relationship separately, ensuring the model knows how to map the aliased column back to its property.

It is also worth noting that aliases are specific to the current query execution. If you later modify the scope to remove the alias, all dependent code expecting tag_name will break. Always verify that any downstream logic relying on attribute names accounts for these changes, especially when sharing models across different parts of an application.

Our network