1

我有一个 Laravel 4 加入,如下所示:

return DB::table(Location::getTableName() . ' as l')->where('l.company_id', $companyId)
        ->join(User::getTableName() . ' as u', 'u.location_id', '=', 'l.id')->whereIn('l.id', $locationsId)
        ->join(UserTimeline::getTableName() . ' as ut', 'ut.user_id', '=', 'u.id')
        ->join(Status::getTableName() . ' as s', 's.id', '=', 'ut.status_id')
        ->select(
            'l.name as location_name',
            'u.first_name as user_first_name',
            'u.last_name as user_last_name',
            'u.email as user_email',
            'ut.started_at as timeline_started_at',
            'ut.finished_at as timeline_finished_at',
            's.id as status_id',
            's.label as status_label'
        )
        ->orderBy('ut.id', 'asc')
        ->skip($from)
        ->limit($limit)
        ->get();

我必须检测用户的时区并将与 GMT 的差异计算为一个数字,巴基斯坦可能是 +5,印度可能是 +5.5。现在我的问题是我正在将此连接的数据导出到 CSV 文件中。我必须在“timeline_started_at”和“timeline_finished_at”中添加小时数。我搜索并发现 SQL 的“DATEADD”方法可以增加或减少小时数。

真正的问题是我不知道在上面的连接中我应该在我的 Laravel 连接中使用 'DATEADD' 函数。

有人可以在这方面帮助我吗?????????

4

2 回答 2

1

您可以使用DB::raw()将 sql 函数添加到查询中。

例如:

$test = DB::table('test')->where(DB::raw('DATE_ADD(created_at, 5)'), '<', $date)->get();
于 2015-12-03T13:24:55.520 回答
1

你可以使用类似的东西:

return DB::table(Location::getTableName() . ' as l')->where('l.company_id', $companyId)
        ->join(User::getTableName() . ' as u', 'u.location_id', '=', 'l.id')->whereIn('l.id', $locationsId)
        ->join(UserTimeline::getTableName() . ' as ut', 'ut.user_id', '=', 'u.id')
        ->join(Status::getTableName() . ' as s', 's.id', '=', 'ut.status_id')
        ->select(
            'l.name as location_name',
            'u.first_name as user_first_name',
            'u.last_name as user_last_name',
            'u.email as user_email',
            'DATE_ADD(ut.started_at, INTERVAL '.$var.' HOUR) as timeline_started_at',
            'DATE_ADD(ut.finished_at, INTERVAL '.$var.' HOUR) as timeline_finished_at',
            's.id as status_id',
            's.label as status_label'
        )
        ->orderBy('ut.id', 'asc')
        ->skip($from)
        ->limit($limit)
        ->get();

其中$var代表小时数。

于 2015-12-03T13:28:10.757 回答