Laravel 中的别名

Alias in Laravel

如何在 laravel 中使用别名?这是我的方法,但我收到了一个错误。

SQLSTATE[42S02]:未找到基础 table 或视图:1146 Table 'Tbl_payroll_employee_basic' 不存在 查询

$data["_leave_info"] = Tbl_payroll_leave_schedulev3::select('*','basicapprover.payroll_employee_display_name','basicreliever.payroll_employee_display_name')
->leftjoin('Tbl_payroll_employee_basic AS basicreliever', 'basicreliever.payroll_employee_id', '=', 'Tbl_payroll_leave_schedulev3.payroll_employee_id_reliever')
    ->leftjoin('Tbl_payroll_employee_basic AS basicapprover', 'basicapprover.payroll_employee_id', '=', 'Tbl_payroll_leave_schedulev3.payroll_employee_id_approver')
    ->get();

Tbl_payroll_leave_schedule_v3 table

+----+--------------+-------------+--------------------------+
| payroll_employee_id_approver | payroll_employee_id_reliever|
+----+--------------+-------------+--------------------------+
|                              |                             |
|             2                |               3             |
+----+--------------+-------------+--------------------------+

Tbl_payroll_employee_basic table

+----+--------------+-------------+--------------------------+
|       payroll_employee_id    | payroll_employee_displayname|
+----+--------------+-------------+--------------------------+
|             2                |     Goku                    |
|             3                |     Naruto                  |
+----+--------------+-------------+--------------------------+

更新

我找到了问题的原因,原因是MISPELLED table Tbl_payroll_employee_basic 应该是 tbl_payroll_employee_baisc。所以一定要仔细检查所有 table 的拼写。

Laravel 支持在带有 AS 的表和列上使用别名。在执行查询之前尝试使用别名并将其存储在变量中并在查询中使用它。下面是 laravel.

中别名的一个工作示例
$users = DB::table('some_very_very_long_table_name AS t')
           ->select('t.id AS uid')
           ->get();

希望对您有所帮助。