Laravel - 添加新的数据到结果 [英] Laravel - add new data into result

查看:195
本文介绍了Laravel - 添加新的数据到结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能得到maxoffers表中的所有MAX-报价:

I have this function to get all max-offers from maxoffers table:

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 $启动日期 $结束日期一天一天。如果没有日期的那一天,我需要添加一个新的对象为 $ maxoffers 数据如下:

I need to go through $maxoffers from $start date to $end date day by day. If there is no date for that day, I need to add a new object into $maxoffers with the following data:

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

所以,我怎么可以顺利通过 $ maxoffers 键,如果没有在期限某些日期从 $启动 $结束然后添加新的数据导致?

So how I can go through $maxoffers and if there is not some date in period from $start to $end then to add new data to result?

更新:

我尝试:

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;
    }

但给我这个错误:

推荐答案

这是简单的版本。你只需要使用日期操纵。

It is ver simple. YOu just need to use Carbon for date manipulation.

 $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();
}

下面只传递日期的有效格式为 $起始日期 $ END_DATE 。在你的情况下,它是有效的所以没有问题存在。现在使用 EQ()的日期比较方法,并添加一天时间每次迭代。我用 $温度作为一个例子。只要你想,你可以定义这个数组的价值观和密钥。既然你不使用您需要遍历到阵列中的每个元素,以查看是否该日期存在与否。

Here Just Pass the valid format of date for $start_date and $end_date. In your case it is valid so no problem there. Now use eq() method for date comparison and add one day for every iteration. I used $temp as an example. You can define this array's values and keys as you want. Since you are not using collection you need to iterate to each element in array to see if that date exists or not.

这篇关于Laravel - 添加新的数据到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆