【问题标题】:How to compare int and timestamp column (Laravel eloquent)?如何比较 int 和时间戳列(Laravel 雄辩)?
【发布时间】:2019-08-27 13:04:31
【问题描述】:

我有以下代码

$firstItem = ['events' => (object)['event_code' => 'reg', 'minutes_from' => 15, 'minutes_to' => 75 ]];
$set = DB::table('customers')->select('customers.idfa', 'customers.idfv', 'customers.app_build_number', 'customers.os_version', 'customers.model')->distinct()
    ->join('customer_events', 'customer_events.customer_id', '=', 'customers.id')
    ->join('customer_event_types', 'customer_events.customer_event_type_id', 'customer_event_types.id')
    //     event      where
    ->when($firstItem, function ($query, $events) {
        foreach ($events as $event) {
            if (!isset($event->event_code) || empty($event->event_code)) {
                continue;
            }

            $query->where('customer_event_types.code', '=', $event->event_code);

            if (isset($event->minutes_from) && !empty($event->minutes_from) && is_numeric($event->minutes_from)) {
                $query->whereTime("customer_events.created_at", '<', Carbon::now()->subMinutes($event->minutes_from)->timestamp);
            }
            if (isset($event->minutes_to) && !empty($event->minutes_to) && is_numeric($event->minutes_to)) {
                $query->whereTime("customer_events.created_at", '>', Carbon::now()->addMinutes($event->minutes_to)->timestamp);
            }
        }
        return $query;
    })
    ->get();

但是当我在tinker 的帮助下运行它时,我看到了以下错误:

Illuminate/Database/QueryException with message 'SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type time: "1566909353" (SQL: select distinct "customers"."idfa", "customers". "idfv", "customers"."app_build_number", "customers"."os_version", "customers"."model" 从 "customers" 内部加入 "customer_events" 到 "customer_events"."customer_id" = "customers"。 id”在“customer_events”上加入“customer_event_types”。“customer_event_type_id”=“customer_event_types”。“id”其中“customer_event_types”。“code”=reg和“customer_events”。“created_at”::time 1566914753)'

我有这方面的 SQL 等效项:

select customers.id, customers.idfa, customers.idfv, count(customer_events.id) as event_counter
      from "customers"
               inner join "customer_events" on "customer_events"."customer_id" = "customers"."id"
               inner join "customer_event_types"
                          on "customer_events"."customer_event_type_id" = "customer_event_types"."id"
      where "customer_event_types"."name" = 'Регистрация'
        and customer_events.created_at <
            (CURRENT_TIMESTAMP - (15 * interval '1 minute')) 
        and customer_events.created_at >
            (CURRENT_TIMESTAMP - (65 * interval '1 minute'))

      group by customers.id, customers.idfa, customers.idfv

它有效。当我使用 -&gt;toSql() 而不是进入 Eloquent 查询时,我看到以下代码:

select distinct "customers"."idfa",
                "customers"."idfv",
                "customers"."app_build_number",
                "customers"."os_version",
                "customers"."model"
from "customers"
         inner join "customer_events" on "customer_events"."customer_id" = "customers"."id"
         inner join "customer_event_types" on "customer_events"."customer_event_type_id" = "customer_event_types"."id"
where "customer_event_types"."code" = 'reg'
  and "customer_events"."created_at" < 1566908749
  and "customer_events"."created_at" > 1566914149

据我所知,这里是与时间戳比较不正确的整数。 如何为 Eloquent 解释我要比较时间戳?

【问题讨论】:

    标签: php laravel postgresql eloquent


    【解决方案1】:

    为什么要将 Carbon 实例转换为时间戳?让 Laravel 查询构建器处理将 Carbon 转换为适合数据库的格式。

    做事

    Carbon::now()->subMinutes($event->minutes_from)
    

    而不是

    Carbon::now()->subMinutes($event->minutes_from)->timestamp
    

    在您的查询中。

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2017-01-25
      • 2023-04-03
      • 2016-06-27
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      相关资源
      最近更新 更多