2017-08-11 14 views
1

我有一個名爲finished_ca表Laravel DB - 通過在其他列加入天Comaparing 2分日期列

表是這樣的:

id date_received  manufacturating_date sample_status 
1  2017-06-08T15:53  2017-07-05    1 
2  2017-06-08T15:49  2017-08-01    2 

如果sample_status = 1的日子,我必須添加30 如果sample_status = 2,我必須添加的日期是14

現在我想獲取所有date_received小於製造日期的id,如果sample_id 1,我要補充的日子是30,如果是2,如果它是寫在一個PHP語言,我將新增30

,它看起來就像這樣:

這只是一個例子壽。

$days = sample_status == 1 ? 30 : 14;

繼承人,我試圖查詢:

/* 
I know that this will not work, but i want to parse the date first, 
and add days depending on the sample_status 
*/ 
$response_data = DB::table('finished_ca') 
->where('date_received', '<=', Carbon::parse('manufacturing_date')->addDays(??)); 

回答

1

鍶,我沒仔細看 這裏我更新的答案:d

$response_data = DB::table('finished_ca') ->where(function($query){ $query->where('sample_status', 1) ->whereRaw('date(date_received) <= DATE_ADD(manufacturing_date, INTERVAL 30 DAY)'); }) ->orWhere(function($query){ $query->where('sample_status', 2) ->whereRaw('date(date_received) <= DATE_ADD(manufacturing_date, INTERVAL 14 DAY)'); })->get();

+0

我想你誤會了我的問題,我正在做一個選擇語句,我不需要更新數據。 – KennethC

+0

@KennethC我更新了我的答案:D –

+0

嗨,我認爲它的工作原理,但我需要先做一些測試,然後將其設置爲接受的答案,謝謝兄弟! – KennethC