无法将传递的值与列名进行比较。 Laravel

Cannot compare passed value with column name. Laravel

我正在尝试使用 where 子句来比较从 id 路由传递的值。为此,我使用了以下代码:

Route::get('Bill/{id}/students',['uses' =>'BillController@student']);

我需要将 id 与控制器中的列名称 grade_id 进行比较。为此,使用了以下代码:

public function student($id)
{
    $data['id'] = $id;      
    $students=Student::all()->where('grade_id',$data);

    return view('bill.students',compact('students'));
}

但它返回的是空值。 如果我传递直接值 1 而不是 $data。它工作得很好。 谁能给我解决方案?

使用whereLoose()代替where()

whereLoose() This method has the same signature as the where method; however, all values are compared using "loose" comparisons

Source

为什么会这样?

$data 有一个 String 类型,你正在比较字符串和 Int 类型,即使你传递数值 $data 将是字符串类型。

(1 === '1') //False 
(1 === 1) //True
(1 == '1') //True