Skip to content

Is it possible to translate this “join” and “as” query to Laravel PDO functions?

I am struggling to make this SQL query to a Laravel PDO query. Does anyone know how I can do this? (Especially the 'AS' statements and the naming in the joins).


SELECT         as model_name,
       models.brand        as model_brand,              as trim_name,
       t.extra_information as trim_extra_information,
       t.price             as trim_price,
       t.popular           as trim_is_popular,
       s.type              as specification_type,
       s.value             as specification_value,              as option_name,
       o.default           as option_default,
       o.remaining         as option_remaining,              as color_name,
       c.hex_code          as color_hex_code,
       c.price_extra       as color_price_extra,
       ll.months           as lease_length_months,
       ll.default          as lease_length_default,
       ll.price_extra      as lease_length_price_extra,             as extra_option_name,
       eo.description      as extra_option_description,
       eo.price_total      as extra_option_price_total,
       eo.price_extra      as extra_option_price_extra,
       m.kilometers        as mileage_kilometers,
       m.default           as mileage_default,
       m.price_extra       as mileage_price_extra,
       m.price_extra_km    as mileage_price_extra_km

FROM `models`
         INNER JOIN trims t on = t.model_id
         INNER JOIN specifications s on = s.trim_id
         INNER JOIN options o on = o.trim_id
         INNER JOIN colors c on = c.trim_id
         INNER JOIN lease_lengths ll on = ll.trim_id
         INNER JOIN extra_options eo on = eo.lease_length_id
         INNER JOIN mileages m on = m.lease_length_id



Directly use join() and select() method like this:

Model::join('trims t', '', '=', 't.model_id')
     ->join('specifications s', '', '=', 's.trim_id')
       '         as model_name',
       'models.brand        as model_brand',
       '              as trim_name',
       't.extra_information as trim_extra_information',
       't.price             as trim_price',
       't.popular           as trim_is_popular',
User contributions licensed under: CC BY-SA
6 People found this is helpful