php - Laravel - make json - add to variable new objects -
i have function max-offers maxoffers table:
public function maxoffers($id) { $offers = maxoffer::where('article_id', $id)->latest()->get(['id', 'price', 'start', 'user_id']); return $offers; }
and this:
[{"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}]
now have alse: $start = 'sun, 03 apr 2016 00:00:00'; $end = 'sat, 23 sep 2016 00:00:00';
how can go day day throuth $offers $start date $end date , if there no date day add $offers new object data:
{"title":,"price":100,"start":"date_which_not_excist into_offers","user_id":8}
so how can go throuth $offers , if there not date in period $start $end add new object json?
i haven't performed following code yet wanted this.
try this:
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 $offer) { if(strtotime($offer->start) == strtotime($start_date)) { $count++; } } if($count == 0) { maxoffer::create(['title' => null, 'price' => '100', 'start' => $start_date, 'user_id' => 8 ]); } } // code update $offers variable before return return $offers; }
Comments
Post a Comment