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

  laravel, list, mysql, php, search
$students = Student::elect('students.*', '')->join('users', 'students.user_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('', '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 of both student and users table has a 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?

Source: Laravel StackOverflow

Leave a Reply