I have an array $rest_id
which have 3 ids, but when i do foreach and put this in my sql query there only 1 value appears when i debug it. Here is the code.
$ids = array();
foreach ($rest_id as $value) {
$ids[] = $value->id;
$nearest_rest = DB::select("SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1");
dd($nearest_rest);
}
I think that you need something like this, if you want to get the nearest restaurant out of 3 ids:
$ids = array();
foreach ($rest_id as $value) {
$ids[] = $value->id;
}
$nearest_rest = DB::select("SELECT *, (3956 * 2 * ASIN(SQRT( POWER(SIN(( 28.5812674 - lat) * pi()/180 / 2), 2) +COS( 28.5812674 * pi()/180) * COS(lat * pi()/180) * POWER(SIN(( 77.3181059 - lng) * pi()/180 / 2), 2) ))) as distance from restaurant_details where id In ('" . implode("','",$ids) . "') having distance order by distance asc limit 1");
dd($nearest_rest);
You need to use $ids = implode(',',$ID)
function to convert array into string.
Then you can use where id in ($ids)
in your query
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.