我想获取学生的全名,但在我的数据库中,我有两个不同的列:first_name
和 last_name。
我想获取这两个列同一时间。
Controller
public function getStudentName($id)
{
$students = DB::table("students")->where("students_class_id", $id)
->pluck("first_name", "id");
return json_encode($students);
}
请您参考如下方法:
在您的 eloquent 模型中创建自定义访问器,例如:
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
然后在查询中使用它:
$students = DB::table("students")->where("students_class_id",$id)->pluck("full_name","id");
用 eloquent like 试试吧:
Student::where("students_class_id",$id)->pluck("full_name","id")->toArray();