如何使用查询构建器在 Laravel Eloquent 查询中给表赋别名?
Eloquent 是一种新的对象关系映射器 (ORM),它有助于与数据库交互。借助 Eloquent,每个表都有一个映射模型,该模型负责对该表上所有操作的处理。
假设我们已经创建了一个名为student的表,其中包含以下内容−
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ | id | name | email | created_at | updated_at | address | age | +----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ | 1 | Siya Khan | [email protected] | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz | 20 | | 2 | Rehan Khan | [email protected] | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz | 18 | | 3 | Rehan Khan | [email protected] | NULL | NULL | testing | 20 | | 4 | Rehan | [email protected] | NULL | NULL | abcd | 15 | | 5 | Nidhi Agarwal | [email protected] | NULL | NULL | abcd | 20 | | 6 | Ashvik Khanna | [email protected] | NULL | NULL | oooo | 16 | | 7 | Viraj Desai | [email protected] | NULL | NULL | test | 18 | | 8 | Priya Singh | [email protected] | NULL | NULL | test123 | 20 | +----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ 8 rows in set (0.00 sec)
示例
在 Laravel 中,可以使用“as”关键字在表上创建别名。以下是一个示例
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index() { echo $student = Student::from( 'students as std' ) ->orderBy('std.name', 'ASC') ->orderBy('std.email', 'ASC') ->get(); } }
输出
上述代码的输出如下
[{"id":2,"name":"Rehan Khan","email":"[email protected]","created_at":"2022-05-01T13:49:50.000000Z","updated_at":"2022-05-01T13:49:50.000000Z","address":"Xyz"},{"id":1,"name":"Siya Khan","email":"[email protected]","created_at":"2022-05-01T13:45:55.000000Z","updated_at":"2022-05-01T13:45:55.000000Z","address":"Xyz"}]
示例 1
现在让我们使用 DB facade 测试别名
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; //use App\Models\Student; use DB; class StudentController extends Controller { public function index() { $users = DB::select('select * from students as std'); print_r($users); } }
输出
Array ( [0] => stdClass Object( [id] => 1 [name] => Siya Khan [email] => [email protected] [created_at] => 2022-05-01 13:45:55 [updated_at] => 2022-05-01 13:45:55 [address] => Xyz ) [1] => stdClass Object( [id] => 2 [name] => Rehan Khan [email] => [email protected] [created_at] => 2022-05-01 13:49:50 [updated_at] => 2022-05-01 13:49:50 [address] => Xyz ) )
示例 2
以下是使用 DB facade 创建表别名的另一种方法−
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; //use App\Models\Student; use DB; class StudentController extends Controller { public function index() { $users = DB::table('students as std') ->get(array('std.name as std_name')); print_r($users); } }
输出
上述程序的输出为 -
Illuminate\Support\Collection Object ( [items:protected] => Array( [0] => stdClass Object( [std_name] => Siya Khan ) [1] => stdClass Object( [std_name] => Rehan Khan ) ) [escapeWhenCastingToString:protected] => )
广告