title: Laravel looping through dates and insert data to database
body: <p>I am looping through 3 cinemas id's and for each one I want to loop for 7 days.</p>
  
  <p>At this moment it is looping through 3 cinemas for today only and also it updates current record so if a cinema is playing film "Wonder Woman" and another cinemas plays the same film, it will merge the times together and that is not what I want.</p>
  
  <p>Each cinema should be associated to business_id and therefore each cinema should have it's own record, even if the name matches.</p>
  
  <blockquote>
    <p>$films = Business::where('type', $type)->get();</p>
  </blockquote>
  
  <p>Is giving me back 3 records like this:</p>
  
  <pre><code>[{"id":2,"name":"hello","type":"3","email":"fsdf","logo":null,"twitter_business":null,"facebook_business":null,"instagram_business":null,"image":null,"gallery_id":null,"user_id":1,"created_at":null,"updated_at":null},{"id":3,"name":"dasd","type":"3","email":"fsdfsdf","logo":null,"twitter_business":null,"facebook_business":null,"instagram_business":null,"image":null,"gallery_id":null,"user_id":1,"created_at":null,"updated_at":null},{"id":4,"name":"dasdas","type":"3","email":"dasdasd","logo":null,"twitter_business":null,"facebook_business":null,"instagram_business":null,"image":null,"gallery_id":null,"user_id":1,"created_at":null,"updated_at":null}]
  </code></pre>
  
  <p>My array that I loop through has 3 entries:</p>
  
  <pre><code>$arrayOfCinemaIds = [10565,7311,9434];
  </code></pre>
  
  <p>So I want to match it so that first entry in array matches first entry from where::</p>
  
  <p>I also want it to insert a new entry when date changes not overwrite it currently I have something like:</p>
  
  <p><a href="https://i.stack.imgur.com/NuZGB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NuZGB.jpg" alt="enter image description here"></a></p>
  
  <p>Taking "Wonder Woman" as example again what I want is something like this:</p>
  
  <pre><code>id=5
  title=Wonder Women
  times=times
  date=today (so 14-06-2017)
  business_id = 4
  
  id=6
  title=Wonder Women
  times=times
  date=tomorrow (so 15-06-2017)
  business_id = 4
  
  id=
  title=Wonder Women
  times=times
  date=today (so 14-06-2017)
  business_id = 3
  </code></pre>
  
  <p>My code so far:</p>
  
  <pre><code>function odeon(){
      $arrayOfCinemaIds = [10565,7311,9434];
      $startdate = strtotime("today");
      $date = date("Y-m-d", $startdate);
      $enddate = strtotime("+7 Days");
      $type = 3;
      $business_id = 1;
      $films = Business::where('type', $type)-&gt;get();
      //echo $films;
      while ($startdate &lt; $enddate) {
          foreach($arrayOfCinemaIds as $id){
          $business_id = $business_id + 1;
      //append your id
      $data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/'.$id.'/?day='.$date);
      $data = json_decode($data);
      $id = 1;
      foreach($data-&gt;listings as $listing){
        $title = $listing-&gt;title;
        $time = implode(', ', $listing-&gt;times);
        $film = Film::where('id', $id)-&gt;first();
        $id = $id + 1;
        // if news is null
        if (!$film) {
          $film = new Film();
        }
        $film-&gt;title = $title;
        $film-&gt;times = $time;
        $film-&gt;business_id = $business_id;
        $film-&gt;date = $date;
        $film-&gt;save();
        $startdate = strtotime("+1 Days", $startdate);
      }
          }
  }
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>I have managed to get it like this:</p>
  
  <pre><code>function odeon(){
      $arrayOfCinemaIds = [10565,7311,9434];
      $startdate = strtotime("today");
      $date = date("Y-m-d", $startdate);
      $enddate = strtotime("+7 Days");
      $type = 3;
      while ($startdate &lt; $enddate) {
          foreach($arrayOfCinemaIds as $id){
      //append your id
      $data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/'.$id.'/?day='.$date);;
      $data = json_decode($data);
      $film_id = 1;
      $films = Business::where('type', $type)-&gt;get();
          foreach ($films as $film) {
              $business_id =  $film-&gt;id;
          foreach($data-&gt;listings as $listing){
              $title = $listing-&gt;title;
              $time = implode(', ', $listing-&gt;times);
              $film = Film::where('id', $id)-&gt;first();
              $film_id = $film_id + 1;
        if (!$film) {
          $film = new Film();
        }
        $film-&gt;title = $title;
        $film-&gt;times = $time;
        $film-&gt;business_id = $business_id;
        $film-&gt;date = $date;
        $film-&gt;save();
              }
          }
        $startdate = strtotime("+1 Days", $startdate);
        $date = date("Y-m-d", $startdate);
      }
  }
  </code></pre>
  
  <p>However it is running throught only 1 cinema so cinema with id '10565' and not the others</p>