0

I'm a beginner in Laravel. I have faced a issue in using a PHP array in a SQL statement. I have a array $waypointsand it contains names of cities.

$waypoints = array("Paris", "Moscow", "London","New York");

And i want to find their corresponding city id by a SQL query. Code is as follow.

$cityname = $waypoints[2];

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

But this query does not get executed. But if i set a string value to variable $cityname manually, it get executed. As example $cityname = "London";. I can't figure out the issue. Help Needed.

8
  • London has the index 2, not 3. We start counting on 0. What is print_r($waypoints)? Commented Mar 29, 2016 at 4:23
  • @ChristianGollhardt London was just an example. Commented Mar 29, 2016 at 4:26
  • I am not familar with laravel, but as long $cityname = "London" works, $waypoints[2] is not "London". See var_dump($waypoints) Commented Mar 29, 2016 at 4:29
  • Do you see any errors? Commented Mar 29, 2016 at 4:39
  • var_dump($waypoints) is as 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) Commented Mar 29, 2016 at 4:45

1 Answer 1

1

You can always use ->toSql(); to get the query string for debugging.

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.