简体   繁体   中英

Can anyone help me to solve an ambiguous error when search?

$students = Student::select('students.*', 'users.email')->join('users', 'students.user_id', 'users.id')->orderBy("id", "desc")->skip($page * $pageSize)->take($pageSize);

    if (request('se') != "" || request('se') != null) {
        $se = request('se');
        $se = str_replace("+", " ", $se);
        $students = $students
            ->where(function ($q) use ($se) {
                $q->where('students.student_number', 'like', '%' .$se. '%')
                    ->orWhere(DB::raw("CONCAT(`first_name`, ' ', `last_name`)"), 'LIKE', '%' . $se . '%')
                    ->orWhere('students.gender', $se)
                    ->orWhere('students.phone', 'like', '%' .$se. '%')
                    ->orWhere('email', 'like', '%' .$se. '%');
            });
    }
    $students = $students->orderBy('id', 'DESC')->get();

when I search in the list I got Integrity constraint violation: 1052 Column 'first_name' in where clause is ambiguous error. This got when join the table because both students and users table have first name and last name columns, but I have retrieved from only students table, I didn't understand why this appears again? Are there any better solution for this?

Change the first_name and last_name to students.first_name and students.last_name respectively

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM