2016-04-19 3 views
1

を追加します。Laravel - JSONを作る - 私はmaxoffersテーブルからすべての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}] 

今私はalseているが: $ start = '日曜日、2014年4月3日00:00:00'; $ end = '土曜日、2014年9月23日00:00:00';

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

それでは、どのように:私は$の終了日に開始日$から$申し出throuth一日一日を行くと、その日は$に追加するには日付がない場合のデータを使用して新しいオブジェクトを提供していますすることができますどのように

私はthrouth $ offerに行き、$ startから$ endまでの期間に日付がない場合は、jsonに新しいオブジェクトを追加することができますか?

+0

はこれを試してみてください。これはCarbon関数なので、Carbon dateを作成する必要があります。 –

+0

私は答えを – Andrew

+0

のように見せてください。 ' $ cond = true; $ 255; $ start = Carbon :: now(); //開始日をここに設定します。 while($ cond){ if($ dateToCheck == $ date){ //見つかった日付 $ cond = false; } $ start-> addDay(); } –

答えて

1

私は次のコードをまだ実行していませんが、あなたが望むものはこのようになります。 **;ループとそれぞれには、開始日に1日** $スタート - > addDay()のようなものを追加し回しながら、あなたが作ることができる

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

hm、いいえ:何かが間違っているようです。 1/1 Carbon.php行のInvalidArgumentException 425: 分離記号が見つかりません 予期しないデータが見つかりました。 予期しないデータが見つかりました。 データがありません – Andrew

関連する問題