当从 table "add_hotels" 获取数据时,除了所需的数据外,我还在 Laravel 中获取了一些 NULL 数组
When fetching data from table "add_hotels" but along with the required data i'm also getting some NULL arrays in Laravel
HotelController内部的result函数
public function result()
{
$data=Input::except(array('_token'));
$city= $data['city'];
$cities_id = DB::table('cities')
->select('id')
->where('cities.city_name', 'LIKE', "%$city%")
->get();
$hotel = array();
foreach ($cities_id as $value) {
$i=$value->id;
$hotel[] = DB::table('add_hotels')
->select('id')
->where('city_id', '=', $i)
->get();
}
var_dump($hotel);
exit();
return view('hotel.result',compact('hotel','city'));
}
这是我得到的结果,但我只需要用红色框标记的数据
试试这个查询:
$cities_id = DB::table('cities')
->where('cities.city_name', 'LIKE', "%$city%")
->Join('add_hotels','add_hotels.city_id','=','cities.id')
->select('add_hotels.id')
->get();
var_dump($cities_id);
HotelController内部的result函数
public function result()
{
$data=Input::except(array('_token'));
$city= $data['city'];
$cities_id = DB::table('cities')
->select('id')
->where('cities.city_name', 'LIKE', "%$city%")
->get();
$hotel = array();
foreach ($cities_id as $value) {
$i=$value->id;
$hotel[] = DB::table('add_hotels')
->select('id')
->where('city_id', '=', $i)
->get();
}
var_dump($hotel);
exit();
return view('hotel.result',compact('hotel','city'));
}
这是我得到的结果,但我只需要用红色框标记的数据
试试这个查询:
$cities_id = DB::table('cities')
->where('cities.city_name', 'LIKE', "%$city%")
->Join('add_hotels','add_hotels.city_id','=','cities.id')
->select('add_hotels.id')
->get();
var_dump($cities_id);