我有一個關于larave eloquent的問題,以(n)min的形式獲取一天的行數,例如 30分鐘
從8:00:00到8:29:59 。
col1 - col2 - col3
col1 - col2 - col3
...
從8:30:00到8:59:59 。
col1 - col2 - col3
鈳1 - 鈳2 - 鈳3
...
...
...
從23:30:00到23:59:59 。
col1 - col2 - col3
col1 - col2 - col3
我實際上需要用laravel qeloquent把一天分成30分鐘的間隔
。是否有辦法不通過回圈和用php映射來完成這個任務, 以提高大表的性能? 如果沒有,那么什么是最好的方法來獲得這樣的結果。
uj5u.com熱心網友回復:
你可以按照以下步驟進行操作:
創建一個
artisan命令crontab注冊一個cron jobN.B: 請看Laravel檔案中的任務調度主題。
uj5u.com熱心網友回復:
我找到了一個解決方案
我做了一個所需時間間隔的陣列, 然后在這個陣列上做了一個回圈 然后對陣列中的每一個鍵,向資料庫進行查詢然后我將陣列中的每一個鍵合并到一起。
然后我合并了輸出
我的特質中的憤怒制造者的代碼
namespace AppTraits。
trait HoursRange {
protected function function range range($lower = 0, $upper = 86400, $step = 3600, $format = '')
{
$times = array() 。
if ( empty( $format ) ) {
$format = 'g:i a';
}
foreach ( range( $lower, $upper, $step ) as $increment ) {
$increment = gmdate( 'H:i', $increment ) 。
list( $hour, $minutes ) = explode( ' :', $increment ) 。
$date = new DateTime( $hour . ':' . $minutes )。)
$times[(string) $increment] = $date-> format( $format ) 。
}
return $times;
}
并像這樣呼叫使范圍
return hoursRange($lower = 28800, $upper = 86400, $step = 1800, $format = 'H。 i:s')。)
這個輸出是這樣的 :
{
08: 00: "08:00:00"。
08:30: "08:30:00"。
09:00: "09:00:00"。
09:30: "09:30:00"。
10:00: "10:00:00"。
10:30: "10:30:00"。
11:00: "11:00:00"。
11:30: "11:30:00"。
12:00: "12:00:00"。
12:30: "12:30:00"。
13:00: "13:00:00"。
13:30: "13:30:00"。
14:00: "14:00:00"。
14:30: "14:30:00"。
15:00: "15:00:00"。
15:30: "15:30:00"。
16:00: "16:00:00"。
16:30: "16:30:00"。
17:00: "17:00:00"。
17:30: "17:30:00"。
18:00: "18:00:00"。
18:30: "18:30:00"。
19:00: "19:00:00"。
19:30: "19:30:00"。
20:00: "20:00:00"。
20:30: "20:30:00"。
21:00: "21:00:00"。
21:30: "21:30:00"。
22:00: "22:00:00"。
22:30: "22:30:00"。
23:00: "23:00:00"。
23:30: "23:30:00"。
00:00: "00:00:00"。
}
結果是好的,但我不確定性能,以及這是否是最好的方法!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/333690.html
標籤:
上一篇:在表中生成不同維度的串列
