【问题标题】:Is it possible to translate this "join" and "as" query to Laravel PDO functions?是否可以将此“join”和“as”查询转换为 Laravel PDO 函数?
【发布时间】:2020-06-11 13:53:21
【问题描述】:

我正在努力使这个 SQL 查询成为 Laravel PDO 查询。有谁知道我该怎么做? (尤其是 'AS' 语句和连接中的命名)。

查询:

SELECT models.name         as model_name,
       models.brand        as model_brand,
       t.name              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,
       o.name              as option_name,
       o.default           as option_default,
       o.remaining         as option_remaining,
       c.name              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,
       eo.name             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 models.id = t.model_id
         INNER JOIN specifications s on t.id = s.trim_id
         INNER JOIN options o on t.id = o.trim_id
         INNER JOIN colors c on t.id = c.trim_id
         INNER JOIN lease_lengths ll on t.id = ll.trim_id
         INNER JOIN extra_options eo on ll.id = eo.lease_length_id
         INNER JOIN mileages m on ll.id = m.lease_length_id

【问题讨论】:

  • 只使用原始查询。但是'AS' 是你最小的问题 - 在 laravel 中没有什么特别之处。
  • @PaulSpiegel Raw Query 如果我希望我的团队可以维护它,它并不是那么好。所以你是说这可以通过 Laravel 函数实现?
  • 直接使用select('models.name as model_name', '....', '...')和使用join()

标签: mysql sql laravel eloquent query-builder


【解决方案1】:

直接使用join()select()这样的方法:

Model::join('trims t', 'models.id', '=', 't.model_id')
     ->join('specifications s', 't.id', '=', 's.trim_id')
     ...
     ->select(
       'models.name         as model_name',
       'models.brand        as model_brand',
       't.name              as trim_name',
       't.extra_information as trim_extra_information',
       't.price             as trim_price',
       't.popular           as trim_is_popular',
       ...
     )
     ->get()

【讨论】:

    【解决方案2】:

    如果您使用名为Model 的雄辩模型类,它会自动设置为查询models 表。

    然后您可以使用以下命令覆盖 Model 类中的默认查询:

    public function newQuery()
    {
        return parent::newQuery()
            ->select(
                'models.name as model_name',
                ...
                'mileages.price_extra_km as mileage_price_extra_km'
            )
            ->join('trims', 'models.id', 'trims.model_id')
            ...
            ->join('mileages', 'lease_lengths.id', 'mileages.lease_length_id');
    

    在我留下三个点的地方填写额外的行,你应该已经在路上了。

    请注意,我已删除您的表别名。您可以像在 SQL 查询代码中一样为您的表设置别名,但使用 eloquent 查询构建器时更简单、更易读。

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2018-10-10
      相关资源
      最近更新 更多