Проблемы с преобразованием необработанного SQL-запроса в Laravel Query - PullRequest
0 голосов
/ 31 октября 2018

Я пытаюсь преобразовать этот запрос SQL

SELECT
    intermediate_routes.city_id,
    intermediate_routes.fk_route_id,
    intermediate_routes.start_time,
    intermediate_routes.end_time,
    routes.bus_id,
    lk_city.city_name,
    bus.bus_no,
    bus.id,
    routes.fk_start_stopes_id,
    routes.fk_end_stopes_id,
    lk_company.company_name

    FROM
        intermediate_routes,
        routes,
        lk_city,
        bus,lk_company
    WHERE
    intermediate_routes.city_id = 310
    AND routes.id = intermediate_routes.fk_route_id
    AND lk_city.id = intermediate_routes.city_id
    AND routes.bus_id = bus.id
    AND bus.fk_company_id = lk_company.id
    AND intermediate_routes.start_time BETWEEN '23:00' and '23:59'
    ORDER BY
        intermediate_routes.start_time

для строителя запросов вот так -

$get = \DB::table('intermediate_routes')
      ->select("intermediate_routes.city_id as city_id", "intermediate_routes.fk_route_id as route_id", "intermediate_routes.start_time as start_time", "intermediate_routes.end_time as end_time", "routes.bus_id as bus_id", "lk_city.city_name as city_name", "bus.bus_no as bus_no", "bus.id as bus_id", "routes.fk_start_stopes_id as start_stop_id", "routes.fk_end_stopes_id as end_stopes_id", "lk_company.company_name as company_name")
      ->join('lk_city', 'intermediate_routes.fk_route_id', '=', 'lk_city.id')
      ->join('routes',  function($route){
        $route->on('intermediate_routes.fk_stopes_id', '=', 'routes.id')
        ->join("bus", function($bus){
          $bus->on('routes.bus_id', '=', 'bus.id')
          ->join('lk_company', 'bus.fk_company_id', '=', 'lk_company.id');
        });
      })
      ->where('intermediate_routes.city_id', $value)
      ->whereBetween("intermediate_routes.start_time", ['23:00', '23:59'])
      ->orderBy('intermediate_routes.start_time')
      ->get();

Я не вижу ошибки при этом, но также получаю результаты, как и ожидалось.

В необработанном запросе я вижу одну строку, возвращенную как результат, но в представлении Laravel я не вижу никакого результата.

1 Ответ

0 голосов
/ 31 октября 2018

Я думаю, что исправил проблему. Я делаю это сейчас, и вижу результат -

$get = \DB::table('intermediate_routes')
      ->join('lk_city', 'lk_city.id', '=', 'intermediate_routes.city_id')
      ->join('routes', 'routes.id', '=', 'intermediate_routes.fk_route_id')
      ->join('bus', 'routes.bus_id', '=', 'bus.id')
      ->join('lk_company', 'bus.fk_company_id', '=', 'lk_company.id')
      ->select("intermediate_routes.city_id as city_id", "intermediate_routes.fk_route_id as route_id", "intermediate_routes.start_time as start_time", "intermediate_routes.end_time as end_time", "routes.bus_id as bus_id", "lk_city.city_name as city_name", "bus.bus_no as bus_no", "bus.id as bus_id", "routes.fk_start_stopes_id as start_stop_id", "routes.fk_end_stopes_id as end_stopes_id", "lk_company.company_name as company_name")
      ->where('intermediate_routes.city_id', $value)
      ->whereBetween("intermediate_routes.start_time", ["23:00", "23:59"])
      ->orderBy('intermediate_routes.start_time')
      ->get(); 
...