【问题标题】:When fetching data from table "add_hotels" but along with the required data i'm also getting some NULL arrays in Laravel从表“add_hotels”中获取数据时,除了所需的数据,我还在 Laravel 中获得了一些 NULL 数组
【发布时间】:2018-01-30 05:30:52
【问题描述】:

是 HotelController 内部的结果函数。

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'));

    }

这是我得到的结果,但我只需要用红色框标记的数据

【问题讨论】:

  • 请看附图以便更好地理解...

标签: php mysql laravel laravel-query-builder


【解决方案1】:

试试这个查询:

$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多