【问题标题】:How can I push items to an array within a laravel query?如何将项目推送到 laravel 查询中的数组?
【发布时间】:2015-11-25 17:25:05
【问题描述】:

我正在运行一些查询,我需要能够将项目推送到数组中,然后将数组项分配给该对象的更新查询。以下是该功能背后的想法以及我认为应该是什么:

"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 个子对象,但我无法获得正确的查询来按照我想要的方式执行此操作。谁能指出我正确的方向?

【问题讨论】:

    标签: php mysql arrays json laravel


    【解决方案1】:

    当您使用$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']);
    

    【讨论】:

      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多