Andrew
Andrew

Reputation: 2198

Laravel - add new data into result

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

and I get 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 I have also:

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

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}

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?

UPDATE:

I try:

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

but give me this error: enter image description here

Upvotes: 1

Views: 189

Answers (1)

nextt1
nextt1

Reputation: 3988

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

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.

Upvotes: 1

Related Questions