我有幾個查詢正在運行,我需要能夠將項目推入數組,然後將數組項目分配到該對象的更新查詢中。下面是功能,我認爲背後的理念應該是:如何將項目推送到laravel查詢中的數組?
"offer_when」: [
{
「when」: 「later」,
"offer_end_time": "19:00",
"offer_start_time": "17:00"
},
{
「when」: 「tomorrow」,
"offer_end_time": "18:00",
"offer_start_time": "17:00"
}
]
的想法是,更新offer_when對象時,我們不只是添加一個我們添加多達我們可以基於這樣的特定條件如:
- 如果報價一天是明天
- 如果報價一天開始時間晚於當前時間 等
到目前爲止,我CA n運行查詢並將其添加到offer_when對象字段中,但它只添加一個值而不是多個值,這是我所需要的。
功能到目前爲止是在foreach循環,我做的所有查詢像這樣的下面:
public function getOffers(Offer $offer)
{
$mytime = Carbon::now('Europe/London');
$currentTime = $mytime->format('H:i');
$today = $mytime->format('m/d/Y');
$day = $mytime->format('l');
$tomorrow = Carbon::now()->addDay(1)->format('l');
$thisDay = strtolower($day);
DB::table('offers')->update(['current_time' => $currentTime]);
DB::table('offers')->update(['current_date' => $today]);
DB::table('offers')->update(['current_day' => $thisDay]);
foreach(Offer::all() as $offerObject){
$when = $offerObject['offer_when'];
$the_days = $offerObject['days'];
$featured = $offerObject['current_date'];
$featured_date = $offerObject['featured_date'];
$start_time = $offerObject['offer_start_time'];
$current_time = $offerObject['current_time'];
$end_time = $offerObject['offer_end_time'];
$whens = array();
$whenDataTomorow =
[
array(
'when' => 'tomorrow',
'start_time' => $start_time,
'end_time' => $end_time
)
];
$whenDataNow =
[
array(
'when' => 'now',
'start_time' => $start_time,
'end_time' => $end_time
)
];
$whenDataLater =
[
array(
'when' => 'later',
'start_time' => $start_time,
'end_time' => $end_time
)
];
$isTomorrow = json_encode($whenDataTomorow);
$isNow = json_encode($whenDataNow);
$isLater = json_encode($whenDataLater);
$offerObject::where('days', 'LIKE', '%' . strtolower($tomorrow) . '%')
->update(['offer_when' => $isTomorrow]);
$offerObject::where('days', 'LIKE', '%' . $thisDay . '%')
->where('current_time', '>', $start_time)
->update(['offer_when' => $isNow]);
$offerObject::where('days', 'LIKE', '%' . $thisDay . '%')
->where('offer_start_time', '>', $current_time)
->update(['offer_when' => $isLater]);
$offerObject::whereBetween('offer_start_time', array('07:00','12:00'))
->update(['types' => 'breakfast']);
$offerObject::whereBetween('offer_start_time', array('11:00','14:00'))
->update(['types' => 'lunch']);
$offerObject::whereBetween('offer_start_time', array('14:00','21:00'))
->update(['types' => 'dinner']);
$offersAll = $offerObject
::where('days', 'LIKE', '%' . $thisDay . '%')
->orWhere('days', 'LIKE', '%' . strtolower($tomorrow) . '%')
->orWhere('featured_date', 'LIKE', '%' . $featured . '%')
->get();
return $offersAll;
}
}
上述作品查詢和我得到的輸出的東西,它似乎拿起正確值,但我需要這個工作的基礎上,一些優惠屬於相同的查詢事實。這是一個提供輸出:
"offer_when": [
{
"when": "now",
"start_time": "08:50",
"end_time": "08:50"
}
],
但是這個對象字段應該有2個對象,但我不能做到這一點,我希望它成爲的方式查詢權。任何人都可以將我指向正確的方向嗎?