簡體   English   中英

如何在Laravel Join中使用'DATEADD'功能?

[英]How to Use 'DATEADD' function in Laravel join?

我有一個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'函數。

有人可以在這方面幫助我嗎?

您可以使用DB::raw()將sql函數添加到查詢中。

例如:

$test = DB::table('test')->where(DB::raw('DATE_ADD(created_at, 5)'), '<', $date)->get();

您可以使用類似這樣的內容:

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表示小時數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM