如何对DB进行多次查询并返回到一个数组

时间:2017-09-18 14:13:34

标签: php mysql laravel laravel-5 laravel-5.5

如何对bd进行多次查询并返回一个数组

最重要的是,搜索从一个城市到另一个城市的门票,还会获取有关公交车品牌的​​信息。

在我的控制器中,我正在执行以下操作:

public function search() {

    // Sets the parameters from the get request to the variables.
    $from_city = Request::get('from');
    $to_city   = Request::get('to');

    //Finds the city ID
    $from_id   = DB::table('city')->where('name', $from_city)->pluck('id');
    $to_id     = DB::table('city')->where('name', $to_city)->pluck('id');
    $date      = Request::get('date');

    //Find a tickets using Query Builder
    $result = DB::table('tickets')
        ->select(DB::raw("*"))
        ->where('from_city_id', '=', $from_id)
        ->where('to_city_id', '=', $to_id)
        ->where('date', '=', $date)
        ->get();


    return view('index.search', ['tickets' => $result]);
}        

另外,我的观点如下:

@foreach ($tickets as $ticket)
    From city id: {{ $ticket->from_city_id }}
    To city id: {{ $ticket->to_city_id }}
    Date: {{ $ticket->date }}
    Bus number: 
    From city name
    To city name:
@endforeach

我需要显示来自标识符的city_name,bus_number数据,它们是在一个单独的数据库表中,我该怎么做才能在视图中显示?

数据库照片:

总线表

bus table

城市表

city table

门票表

tickets table

我将非常感谢您的帮助!

0 个答案:

没有答案