如何从 laravel 7 API 中的两个表中 select 特定列?
How to select specific columns from two tables in laravel 7 API?
我是 Laravel 的新手,正在尝试 select 使用 leftJoin
来自两个不同表的特定列:
public function show(Request $request, Employee $employee)
{
$empno = Employee::find($employee);
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $empno)
->get();
return $employees;
}
这是我的要求:
http://testlaravel.test/api/employees/EMP1987
但它仅 returns:
[]
有什么建议吗?谢谢。
我认为你的代码有误。
第一个:
public function show(Request $request, Employee $employee)
这意味着你有一个参数是 Employee 模型。所以你可以从这个模型中获取数据,而不需要寻找其他的:
public function show(Request $request, Employee $employee)
{
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $employee->empno)
->get();
return $employees;
}
第二个:
Employee::find()
作用于主键,如果你通过
将模型中的主键设置为 id 以外的东西
protected $primaryKey = 'empno';
然后 find 将改为按该键搜索。
所以您的代码可能是:
$empno = Employee::find($employee->empno);
或
$empno = Employee::find($employee->id);
最后,您的代码可能是这样的:
public function show(Request $request, Employee $employee)
{
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $employee->empno)
->get();
return $employees;
}
或
public function show(Request $request, int $empno)
{
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $empno)
->get();
return $employees;
}
或
public function show(Request $request, Employee $employee)
{
$empno = Employee::find($employee->empno);
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $empno->empno)
->get();
return $employees;
}
如果要测试数据,使用LaravelDebugbar或dd();命令 [dd($empno);]
我是 Laravel 的新手,正在尝试 select 使用 leftJoin
来自两个不同表的特定列:
public function show(Request $request, Employee $employee)
{
$empno = Employee::find($employee);
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $empno)
->get();
return $employees;
}
这是我的要求:
http://testlaravel.test/api/employees/EMP1987
但它仅 returns:
[]
有什么建议吗?谢谢。
我认为你的代码有误。
第一个:
public function show(Request $request, Employee $employee)
这意味着你有一个参数是 Employee 模型。所以你可以从这个模型中获取数据,而不需要寻找其他的:
public function show(Request $request, Employee $employee)
{
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $employee->empno)
->get();
return $employees;
}
第二个:
Employee::find()
作用于主键,如果你通过
protected $primaryKey = 'empno';
然后 find 将改为按该键搜索。
所以您的代码可能是:
$empno = Employee::find($employee->empno);
或
$empno = Employee::find($employee->id);
最后,您的代码可能是这样的:
public function show(Request $request, Employee $employee)
{
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $employee->empno)
->get();
return $employees;
}
或
public function show(Request $request, int $empno)
{
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $empno)
->get();
return $employees;
}
或
public function show(Request $request, Employee $employee)
{
$empno = Employee::find($employee->empno);
$employees = DB::table('employees')
->select('employees.empno','employees.firstname', 'employees.lastname','employee_backgrounds.email','employees.remarks')
->leftJoin('employee_backgrounds', 'employees.empno', '=', 'employee_backgrounds.empno')
->where('employees.empno', '=', $empno->empno)
->get();
return $employees;
}
如果要测试数据,使用LaravelDebugbar或dd();命令 [dd($empno);]