Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to alias a table in Laravel Eloquent queries using Query Builder?
Laravel Eloquent provides several ways to alias tables in database queries, making your code more readable and enabling complex joins. Table aliases are particularly useful when working with multiple tables or when you need to shorten long table names.
Sample Data
Let's assume we have a students table with the following data
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ | id | name | email | created_at | updated_at | address | age | +----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ | 1 | Siya Khan | siya@gmail.com | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz | 20 | | 2 | Rehan Khan | rehan@gmail.com | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz | 18 | | 3 | Nidhi Agarwal | nidhi@gmail.com | NULL | NULL | abcd | 20 | +----+---------------+------------------+-----------------------------+-----------------------------+---------+------+
Using Eloquent Model with from() Method
You can alias a table using the You can also create table aliases using raw SQL queries through the DB facade The most flexible approach is using the Query Builder with the Table aliases in Laravel improve code readability and are essential for complex queries involving joins. Use from()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller {
public function index() {
$students = Student::from('students as std')
->orderBy('std.name', 'ASC')
->orderBy('std.email', 'ASC')
->get();
return $students;
}
}
?>
[
{"id":2,"name":"Rehan Khan","email":"rehan@gmail.com","created_at":"2022-05-01T13:49:50.000000Z","updated_at":"2022-05-01T13:49:50.000000Z","address":"Xyz","age":18},
{"id":1,"name":"Siya Khan","email":"siya@gmail.com","created_at":"2022-05-01T13:45:55.000000Z","updated_at":"2022-05-01T13:45:55.000000Z","address":"Xyz","age":20}
]
Using DB Facade with Raw SQL
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class StudentController extends Controller {
public function index() {
$users = DB::select('select * from students as std where std.age > 18');
return $users;
}
}
?>
Array (
[0] => stdClass Object (
[id] => 1
[name] => Siya Khan
[email] => siya@gmail.com
[created_at] => 2022-05-01 13:45:55
[updated_at] => 2022-05-01 13:45:55
[address] => Xyz
[age] => 20
)
[1] => stdClass Object (
[id] => 3
[name] => Nidhi Agarwal
[email] => nidhi@gmail.com
[created_at] =>
[updated_at] =>
[address] => abcd
[age] => 20
)
)
Using Query Builder with table() Method
table() method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class StudentController extends Controller {
public function index() {
$users = DB::table('students as std')
->select('std.name as student_name', 'std.email')
->where('std.age', '>=', 20)
->get();
return $users;
}
}
?>
[
{"student_name":"Siya Khan","email":"siya@gmail.com"},
{"student_name":"Nidhi Agarwal","email":"nidhi@gmail.com"}
]
Comparison
Method
Use Case
Flexibility
Model::from()Eloquent with aliases
Medium
DB::select()Raw SQL queries
High
DB::table()Query Builder
High
Conclusion
from() with Eloquent models, or DB::table() for more flexibility with the Query Builder.
