1

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 alse: $start = 'Sun, 03 Apr 2016 00:00:00'; $end = 'Sat, 23 Sep 2016 00:00:00';

How I can go day by day throuth $offers from $start date to $end date and if there is no date for that day to add into $offers new object with data:

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

So how I can go throuth $offers and if there is not some date in period from $start to $end then to add new object to json?

3
  • You can make while loop and in each turn add one day on start date, something like $start->addDay(); . You need to make Carbon date, because this is a Carbon function. Commented Apr 22, 2016 at 9:16
  • please show me examle as a answer Commented Apr 22, 2016 at 9:18
  • Something like: ` $cond=true; $dateToCheck=something; $start=Carbon::now(); //make your start date here while($cond){ if($dateToCheck==$date){ //found date $cond=false; } $start->addDay(); } ` Commented Apr 22, 2016 at 9:43

1 Answer 1

1

I haven't performed the following code yet but what you wanted would look like 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 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;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

hm, no: Whoops, looks like something went wrong. 1/1 InvalidArgumentException in Carbon.php line 425: The separation symbol could not be found Unexpected data found. Unexpected data found. Data missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.