Laravel - 将新数据添加到结果中

时间:2016-04-21 17:47:43

标签: php arrays json laravel eloquent

我有这个功能来获取maxoffers表中的所有最大优惠:

public function maxoffers($id)
{    
    $offers = Maxoffer::where('article_id', $id)->latest()->get(['id', 'price', 'start', 'user_id']);    
    return $offers;
}

我得到了这个:

 [{"id":121,"price":67,"start":"Sat, 23 Apr 2016 00:00:00 +0000","user_id":8},{"id":114,"price":45,"start":"Sun, 08 May 2016 00:00:00 +0000","user_id":9},{"id":113,"price":53,"start":"Sun, 24 Apr 2016 00:00:00 +0000","user_id":8},{"id":111,"price":55,"start":"Wed, 01 Jun 2016 00:00:00 +0000","user_id":11},{"id":110,"price":53,"start":"Fri, 03 Jun 2016 00:00:00 +0000","user_id":8},{"id":107,"price":53,"start":"Wed, 03 Aug 2016 00:00:00 +0000","user_id":8},{"id":106,"price":53,"start":"Mon, 01 Aug 2016 00:00:00 +0000","user_id":8},{"id":105,"price":53,"start":"Tue, 16 Aug 2016 00:00:00 +0000","user_id":8},{"id":104,"price":55,"start":"Thu, 21 Apr 2016 00:00:00 +0000","user_id":11},{"id":101,"price":57,"start":"Wed, 17 Aug 2016 00:00:00 +0000","user_id":8}]

现在我也有:

$start = 'Sun, 03 Apr 2016 00:00:00';
$end = 'Sat, 23 Sep 2016 00:00:01';

我需要每天从$maxoffers日期到$start日期$end进行审核。如果当天没有日期,我需要使用以下数据将新对象添加到$maxoffers中:

 {"title":,"price":100,"start":"DATE_WHICH_NOT_EXCIST INTO_OFFERS","user_id":8}

那么我如何浏览$maxoffers,如果从$start$end的句点中没有某个日期,那么要将新数据添加到结果中?

更新

我试试:

public function maxoffers($id)
    {

        $start_date = ;
        $end_date = ;

        $offers = Maxoffer::where('article_id', $id)
                            ->where('start', '>=', $start_date)
                            ->where('start', '<=', $end_date)
                            ->get(['id', 'price', 'start', 'user_id']);

        $start_date = 'Sun, 03 Apr 2016 00:00:00';

        $end_date = 'Sat, 23 Sep 2016 00:00:00';

        while (strtotime($start_date) <= strtotime($end_date)) {

            $start_date = date ("Y-m-d", strtotime("+1 day", strtotime($start_date)));

            $count = 0;

            foreach($offers as $offer) {
                if(strtotime($offer->start) == strtotime($start_date)) {
                    $count++;
                }
            }

            if($count == 0) {
                Maxoffer::create(['title' => null, 'price' => '100', 'start' => $start_date, 'user_id' => 8 ]);   
            }

        }

        // do some code to update $offers variable before you return it

        return $offers;
    }

但是给我这个错误: enter image description here

1 个答案:

答案 0 :(得分:1)

这很简单。你只需要使用Carbon进行日期操作。

 $offers  = []; //for example I am using an empty array

$start_date = \Carbon\Carbon::parse('Sun, 03 Apr 2016 00:00:00');
$end_date = \Carbon\Carbon::parse('Sat, 23 Sep 2016 00:00:00');


while(!$start_date->eq($end_date))
{
   $flag = true;

   foreach ($offers as $offer)
   {
     if ($offer['start'] == $start_date->toRfc2822String())
     {
        $flag = false;
        break;
     }
   }

    if($flag)
    {
      $temp = [];
      $temp['title'] = "Title ";
      $temp['price'] = "100";
      $temp['start'] = $start_date->toDateString();
      $temp['user_id'] = 8;

      array_push($offers,$temp);
    }
    $start_date->addDay();
}

此处只传递$start_date$end_date的有效日期格式。在你的情况下它是有效的,所以没有问题。现在使用eq()方法进行日期比较,并为每次迭代添加一天。我以$temp为例。您可以根据需要定义此数组的值和键。由于您没有使用collection,因此需要迭代到数组中的每个元素以查看该日期是否存在。