Warm tip: This article is reproduced from serverfault.com, please click

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

发布于 2020-12-07 14:26:51

When I display the name of table using the id of another table in Laravel I declare like this. But when I want to get the id to edit, the error is like the title. Please help me, I am very grateful.

Controller public function manage_departments(){

    $manage_departments=DB::table('departments')
    ->join('faculties','faculties.id','=','departments.faculty_id')
    ->orderBy('departments.created_at','desc')->get();

    $all_manage_departments=view('admin.manage-departments')->with('manage_departments', $manage_departments);

    return view('layouts.master')->with('admin.manage-departments', $all_manage_departments);
}
Questioner
sirius360
Viewed
0
BABAK ASHRAFI 2020-12-07 22:38:11

You don't have select in your query builder. As laravel doc said, you can specify what do you want to select in select method. I select faculties.id and departments.id for example.

DB::table('departments')
    ->select('faculties.id','departments.id')
    ->join('faculties','faculties.id','=','departments.faculty_id')
    ->orderBy('departments.created_at','desc')->get();