我目前正在创建一个laravel vue spa,只是想知道如何使用这些具有相同结构的指定id获得指定名称。这是标识id的json:
[
[
1,
5
],
[
1,
3
]
]
这是我在EmployeesController.php
:中的getDesignations
函数
public function getDesignations($id) {
$employee_designation_ids = Employees::find($id)->pluck('designation_id')->toArray();
$designation_name = [];
foreach ($employee_designation_ids as $employee_designation_id) {
$designation = Designations::where('id', '=', $employee_designation_id);
//$designation_name[] = $designation;
}
return $employee_designation_ids;
}
如果你特别想要这种格式,你可以这样做(我有很多猜测,因为你没有共享你的表结构(
public function getDesignations($id) {
$employee_designation_ids = Employees::find($id)->designation_id;
return [[$id, $employee_designation_ids]];
}
但是,为什么要为单行结果返回一个双表呢。
感谢您的帮助!我已经设法在我的控制器中用这个方法修复了它:
public function getDesignations(Request $request, $id) {
$employee_designation_ids = Employees::where('id', $id)->pluck('designation_id');
return Designations::whereIn('id', $employee_designation_ids[0])->pluck('name');
}