0

如何对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 数据,它们位于单独的数据库表中,如何才能在视图中显示?

数据库照片:

巴士表

总线表

城市表

城市表

门票表

门票表

我将非常感谢您的帮助!

4

0 回答 0