dongruyan4948 2015-08-31 15:56
浏览 34

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

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:

  • If offer day is tomorrow
  • If offer day start time is later than the current time etc

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?

  • 写回答

1条回答 默认 最新

  • douxie2007 2015-09-04 10:47
    关注

    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']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测