2015-03-18 44 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