I have a few queries I am running and I need to be able to push items into an array and then assign the array items into an update query on that object. Below is the idea behind the function and what I think it should be:
"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"
}
]
The idea is that when updating the offer_when object we don't just add one we add as many as we can based on certain conditions such as:
So far I can run the query and its adds to the offer_when object field but it only adds one value and not multiple values which is what I need.
The function so far is in a foreach loop and I do all queries this way like the below:
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;
}
}
The query above works and I get something in the output and it seems to pick up the correct values but I need this to work based on the fact that some offers fall into the same queries. This is what outputs on one offer:
"offer_when": [
{
"when": "now",
"start_time": "08:50",
"end_time": "08:50"
}
],
But this object field should have 2 child objects but I cannot get the query right to do this the way I want it to be. Can anyone point me in the right direction?
When you use $model->update()
, it overwrite current value. So you will never get multiple children.
First, don't use json_encode()
to store an array. Use attribute casting.
class Offer extends Model {
// ...
protected $casts = [
'offer_when' => 'array',
];
// ...
}
Now you can treat $offer->offer_when
as an array. Note that you cannot modify the attribute value directly like $offer->offer_when[0] = 1
, this is impossible and system will throw an exception. You have to get current value, manipulate, and assign back to that attribute.
$offerWhen = $offer->offer_when;
// Add
$offerWhen[] = [
'when' => 'now',
'start_time' => $start_time,
'end_time' => $end_time,
];
// Remove
unset($offerWhen[0]);
// Finish? Assign back to the attribute
$offer->offer_when = $offerWhen;
$offer->save();
// Or this also works
$offer->update(['offer_when' => $offerWhen]);
And I suggest to use your when
field as a key for easy access and manipulation.
// Add
$offerWhen['now'] = [
'when' => 'now',
'start_time' => $start_time,
'end_time' => $end_time,
];
// Remove
unset($offerWhen['now']);