Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-生成json-添加到变量新对象_Php_Json_Laravel_Eloquent_Php Carbon - Fatal编程技术网

Php Laravel-生成json-添加到变量新对象

Php Laravel-生成json-添加到变量新对象,php,json,laravel,eloquent,php-carbon,Php,Json,Laravel,Eloquent,Php Carbon,我使用此函数从maxoffers表中获取所有max offers: public function maxoffers($id) { $offers = Maxoffer::where('article_id', $id)->latest()->get(['id', 'price', 'start', 'user_id']); return $offers; } 我明白了: [{"id":121,"price":67,"start

我使用此函数从maxoffers表中获取所有max offers:

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='2016年4月3日星期日00:00:00'; $end=‘2016年9月23日星期六00:00:00’

如何从$start date到$end date逐日通过$offers,如果没有该天的日期添加到$offers新对象和数据中:

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

那么,我如何通过$offers,如果$start到$end之间没有日期,那么就向json添加新对象?

我还没有执行以下代码,但您想要的是这样的

试试这个:

 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;
    }
公共函数maxoffers($id)
{
$start_date=;
$end_date=;
$offers=Maxoffer::where('article_id',$id)
->其中('start','>=',$start\u date)

->where('start','hm,no:Whoops,看起来好像出了什么问题。Carbon.php第425行中的1/1 InvalidArgumentException:找不到分隔符号找到了意外数据。找到了意外数据。您可以在while循环中丢失数据,并在每个循环中在开始日期添加一天,类似于$start->addDay();。您需要设置碳日期,因为这是一个碳函数。请给我一个答案,例如:`$cond=true;$dateToCheck=something;$start=Carbon::now();//在这里设置开始日期,而($cond){if($dateToCheck=$date){//found date$cond=false;}$start->addDay();}`