1

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?

2
  • What is assigned to $this->roomList does it already contain some query setup ? Commented Nov 14, 2019 at 10:37
  • @Mike It's a model. Commented Nov 14, 2019 at 13:00

2 Answers 2

1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

1

MySQL considers the value as integer for field type is integer for e.g

if id = 2 and you enter id = "2test" the output will be the id match in the database, but if you put id = "test2" then it will returns nothing.

Also you can check the same thing in MySQL query.

Please check this question.

if you need to match exactly then first you need to cast that field as char in laravel.

$this->roomList->whereRaw("CAST(id AS CHAR(12))", $id)->get()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.