如何在laravel查询中将项目推送到数组?

时间:2015-08-31 15:56:02

标签: php mysql arrays json 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对象时,我们不会添加一个我们根据某些条件添加的内容,例如:

  • 如果提议日是明天
  • 如果报价日开始时间晚于当前时间 等

到目前为止,我可以运行查询并将其添加到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个子对象,但我无法按照我希望的方式获得正确的查询。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

使用$model->update()时,它会覆盖当前值。所以你永远不会有多个孩子。

首先,不要使用json_encode()来存储数组。使用attribute casting

class Offer extends Model {
   // ...

   protected $casts = [
       'offer_when' => 'array',
   ];

   // ...
}

现在您可以将$offer->offer_when视为数组。请注意,您不能像$offer->offer_when[0] = 1那样直接修改属性值,这是不可能的,系统将抛出异常。您必须获取当前值,操纵并分配回该属性。

$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]);

我建议您使用when字段作为轻松访问和操作的键。

// Add
$offerWhen['now'] = [
    'when' => 'now',
    'start_time' => $start_time,
    'end_time' => $end_time,
];

// Remove
unset($offerWhen['now']);