Search code examples
laravel-5eloquentget

Laravel query getting data even it doesn't match


I have created a get request function that gets the id from the url http://localhost/user/list?id=1. And now as per testing, even though it has characters like http://localhost/user/list?id=1DFasdf, it retrieves the data values of column 1. I'm a bit confused to this one since I'm new to this issue:

public function lists(Request $request)
{
    // $id value is 1 or 1DFasdf
    $id = $request->id;
    dd($this->roomList->where('id', $id)->get());
}

so when I display the result with or without the dummy characters, it retrieves the data of column 1, but if I use other id (that is not existing in db) or characters first, it works as expected. How can I prevent this one from happening?


Solution

  • You should read about type juggling:-

    public function lists(Request $request)
    {
        // $id value is 1 or 1DFasdf
        $id = $request->id;
        dd($this->roomList->where('id','like', $id)->get());
    }