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.
Londonhas the index2, not3. We start counting on0. What isprint_r($waypoints)?Londonwas just an example.$cityname = "London"works,$waypoints[2]is not"London". Seevar_dump($waypoints)array (size=4)' 0 => string 'Paris' (length=5) 1 => string 'Moscow' (length=6) 2 => string 'London' (length=6) 3 => string 'New York' (length=8)