2015-03-18 43 views
0

我試圖獲取給定日期範圍的數據庫值。問題是,我也試圖實現缺失值。如果我有1天,3個值,我問我想要得到的東西喜歡本作的第1-5天的值:Laravel:零值之間的日期

1 - >值

2 - > NULL/0

3 - >值

4 - > NULL/0

5 - > NULL/0

好吧,我知道如何只用MySQL來解決這個問題,並在左側加入該臨時表:

(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from                        
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, 
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, 
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, 
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, 
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v 

不幸的是我不知道如何將它包含在Eloquent ORM中。 目前我想的是這樣的:

$weekclockings = $shift->clockings()->leftJoin(function($query) 
{                 
    $query->raw($aboveQuery); 
}, "v.selected_date", ">", "timeclocked.time_in") 

但是,這並不工作,只是給了我「在Grammar.php線33 ErrorException:類封閉的對象無法被轉換成字符串」。

所以,這可能不是最好的辦法。有沒有人有一個想法如何實現這一目標?

+0

我不明白,你混合(順序?)的一部分日期值與日期值。當允許空值時,任何人都可以區分應該放在什麼範圍內?我的意思是,如果你知道你想要使用簡單的地方,並訂購它的日子... – Kyslik 2015-03-18 22:34:07

回答

0

我不知道什麼是clockings()在這裏 - 可能是一個範圍,但使用原始SQL的加入,你應該使用:

$weekclockings = $shift->clockings()->leftJoin(\DB::raw($aboveQuery), "v.selected_date", ">", "timeclocked.time_in"); 
+0

clockings()是一種關係。你的代碼工作,謝謝! – mietzekotze 2015-03-19 22:04:16