0

I'm a beginner in Laravel. I have faced a issue in using a PHP array value inside a SQL query statement. I have a array $waypoints and it contains names of cities. And var_dump($waypoints) looks like follows.

array (size=4)
0 => string 'Paris' (length=5) 
1 => string 'Moscow' (length=6) 
2 => string 'London' (length=6) 
3 => string 'New York' (length=8)

And I am trying to find the corresponding idof a city by executing a SQL query. The code is as follow.

    $cityname = $waypoints[2];

    $city = City::where('name', 'LIKE', "$cityname%")->firstOrFail();

This query does not get executed.

var_dump($city) is like this.

string 'select * from 'cities' where 'name' LIKE ? ' (length=42)

But if i set a string value to variable $cityname manually(As example $cityname = "London";), it get executed.

I can't figure out the issue. Help Needed.

1
  • 1
    try this.. $city = City::where('name', 'LIKE', $cityname."%")->toSql(); Commented Apr 1, 2016 at 3:49

1 Answer 1

1

you should do this

 $city = City::where('name', 'LIKE', $cityname."%")->firstOrFail();

or

 $city = City::where('name', 'LIKE', "{$cityname}%")->firstOrFail();

instead of

 $city = City::where('name', 'LIKE', "$cityname%")->firstOrFail();
Sign up to request clarification or add additional context in comments.

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.