Laravel循环日期并将数据插入数据库

时间:2017-06-14 12:48:12

标签: laravel laravel-5

我正在循环播放3个电影院,每个我想循环播放7天。

此时它仅在今天循环播放3家影院,并且它还更新了当前的记录,因此如果电影院正在播放电影"神奇女侠"而另一家电影院播放同一部电影,它会将时间合并在一起,这不是我想要的。

每个影院都应该与business_id相关联,因此即使名称匹配,每个影院也应该拥有自己的影片。

  

$ films = Business :: where(' type',$ type) - > get();

回馈3条记录:

[{"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}]

我循环播放的数组有3个条目:

$arrayOfCinemaIds = [10565,7311,9434];

所以我想匹配它,以便数组中的第一个条目匹配from ::

的第一个条目

我还希望它在日期更改未覆盖时插入新条目,目前我有类似的内容:

enter image description here

采取"神奇女侠"再例如我想要的是这样的:

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

到目前为止我的代码:

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)->get();
    //echo $films;
    while ($startdate < $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->listings as $listing){
      $title = $listing->title;
      $time = implode(', ', $listing->times);
      $film = Film::where('id', $id)->first();
      $id = $id + 1;
      // if news is null
      if (!$film) {
        $film = new Film();
      }
      $film->title = $title;
      $film->times = $time;
      $film->business_id = $business_id;
      $film->date = $date;
      $film->save();
      $startdate = strtotime("+1 Days", $startdate);
    }
        }
}

修改

我设法得到这样:

function odeon(){
    $arrayOfCinemaIds = [10565,7311,9434];
    $startdate = strtotime("today");
    $date = date("Y-m-d", $startdate);
    $enddate = strtotime("+7 Days");
    $type = 3;
    while ($startdate < $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)->get();
        foreach ($films as $film) {
            $business_id =  $film->id;
        foreach($data->listings as $listing){
            $title = $listing->title;
            $time = implode(', ', $listing->times);
            $film = Film::where('id', $id)->first();
            $film_id = $film_id + 1;
      if (!$film) {
        $film = new Film();
      }
      $film->title = $title;
      $film->times = $time;
      $film->business_id = $business_id;
      $film->date = $date;
      $film->save();
            }
        }
      $startdate = strtotime("+1 Days", $startdate);
      $date = date("Y-m-d", $startdate);
    }
}

然而,只有1个电影院才能播放,所以电影院内有id&#109;&#39;而不是其他人

2 个答案:

答案 0 :(得分:0)

将此$film->date = $date;更改为$film->date = $startdate;此语句将提供更新日期并保存。

答案 1 :(得分:0)

剪切while ($startdate < $enddate) {行并在foreach ($data->listings as $listing) {后粘贴,然后在foreach ($data->listings as $listing) {此行$startdate = $date;之前添加一行。如果您不理解,请查看以下代码。

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)->get();
    //echo $films;

    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;
        $startdate = $date;
        foreach ($data->listings as $listing) {
            while ($startdate < $enddate) {
                $title = $listing->title;
                $time = implode(', ', $listing->times);
                $film = Film::where('id', $id)->first();
                $id = $id + 1;
                // if news is null
                if (!$film) {
                    $film = new Film();
                }
                $film->title = $title;
                $film->times = $time;
                $film->business_id = $business_id;
                $film->date = $startdate;
                $film->save();
                $startdate = strtotime("+1 Days", $startdate);
            }
        }
    }
}