How to alias a table in Laravel Eloquent queries (or using Query Builder)?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Streamlining Laravel Eloquent Queries with Table Aliasing: A Comprehensive Guide Introduction: In the world of modern software development, efficiency and readability are crucial for creating effective, scalable applications. Laravel's query builder offers a powerful toolset to interact with database tables. However, in some scenarios, you might find yourself repeating long table names or struggling with complex select statements. Enter table aliasing: a technique that enhances the readability and maintainability of your queries while reducing redundancy. This blog post will cover everything you need to know about using aliases in Laravel Eloquent queries, both using Eloquent models and the Query Builder. 1. Using Aliases with Eloquent Models: Let's assume that we have a table named "really_long_table_name." Instead of typing this lengthy name every time you need to reference it in your application, you can create an alias for it and use it consistently throughout your code. Here's how it works with Eloquent models: 1. Create a Model Class: Start by creating a model class for the table you want to work with. In our case, we could create a new model named "ShortTable".
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ShortTable extends Model {
    protected $table = 'really_long_table_name'; // Set the actual table name here.
}
2. Use the Alias in Your Code: Now that you have defined a model with an alias, you can use it in your Eloquent queries with ease. Instead of typing "really_long_table_name" every time, you will be able to reference "ShortTable". Here's a simple example using select statements and getting results from the table:
$users = ShortTable::select('id')
                       ->get();
This code snippet translates to: Select all the ID values of records present in the "really_long_table_name" table. The use of an alias "ShortTable" here has reduced the complexity and improved readability substantially. 2. Using Aliases with Laravel's Query Builder: If you don't wish to create a new model for your table, or if you need to make changes directly through Query Builder, there are alternatives available. You can use aliases in your queries by following these steps: 1. Create an Alias for the Base Table Name: In the same manner as described above, define an alias for your base table name. Let's continue using "really_long_table_name" as our example. 2. Use the Alias in Your Query: With your newly created alias defined, you can use it in your Laravel query builder functions like DB::select(), table(), or select(). Here's an example with a simple query that counts the number of users:
$count = DB::table('really_long_table_name AS short_name')
                 ->selectRaw('COUNT(id) AS total_users')
                 ->get();
This query translates to: Count the number of users in the "short_name" alias table (which is actually our "really_long_table_name"). The result will be stored in $count, and we've also added a column alias for clarity. Your Laravel application should now handle aliased queries with ease! Conclusion: Using table aliases in your Laravel Eloquent queries can improve the readability of your code and reduce repetitive typing. With clear naming conventions and proper usage, you can create more maintainable, scalable applications. By following the steps outlined above, you'll be well on your way to a more efficient database management process in Laravel. Remember to keep it consistent across your project, and always seek opportunities for optimization!