I want to get a student's full name, but in my database, I have two different columns: first_name
and last_name.
I want to get both of these columns at the same time.
Controller
public function getStudentName($id)
{
$students = DB::table("students")->where("students_class_id", $id)
->pluck("first_name", "id");
return json_encode($students);
}
Create custom accessor in your eloquent model like :
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
Then use it in the query like :
$students = DB::table("students")->where("students_class_id",$id)->pluck("full_name","id");
Try it with eloquent like :
Student::where("students_class_id",$id)->pluck("full_name","id")->toArray();