Laravel块数据

时间:2018-04-04 07:07:51

标签: mysql laravel

问题是如何在任何情况下将em分成相同的5个部分。所以这意味着如果我采取41.我只需要拆分em 5部分。任何输入必须分为5个部分。 每个$collection取5个。我正在使用大块但是我被困了请求帮助。

结果

array:31 [▼
  0 => Question {#516 ▶}
  1 => Question {#517 ▶}
  2 => Question {#565 ▶}
  3 => Question {#566 ▶}
  4 => Question {#567 ▶}
  5 => Question {#615 ▶}
  6 => Question {#616 ▶}
  7 => Question {#617 ▶}
  8 => Question {#618 ▶}
  9 => Question {#619 ▶}
  10 => Question {#667 ▶}
  11 => Question {#668 ▶}
  12 => Question {#669 ▶}
  13 => Question {#670 ▶}
  14 => Question {#671 ▶}
  15 => Question {#672 ▶}
  16 => Question {#673 ▶}
  17 => Question {#714 ▶}
  18 => Question {#715 ▶}
  19 => Question {#764 ▶}
  20 => Question {#765 ▶}
  21 => Question {#766 ▶}
  22 => Question {#767 ▶}
  23 => Question {#768 ▶}
  24 => Question {#813 ▶}
  25 => Question {#814 ▶}
  26 => Question {#815 ▶}
  27 => Question {#816 ▶}
  28 => Question {#861 ▶}
  29 => Question {#862 ▶}
  30 => Question {#863 ▶}
]

控制器@创建

dd($questions->all());

此处更新是我的Controller @ create

我是如何从视角中提问的。

$all = Request()->all();

        $questions = collect();
        $topicIds = [];
        foreach ($request->input('number') as $key => $value) {
            if (strlen($value) > 0) {
                $topicIds[] = $key;
                $items = Question::inRandomOrder()->where('topic_id', $key)->limit($value)->get();
                //dd($items);
                $questions = $questions->merge($items);
            }
        }

查看

<div class="panel panel-default">
            <div class="panel-heading">
                Сэдэв сонголт
            </div>

            <div class="panel-body">
            @foreach ($duplicates as $duplicate)
            {{ $duplicate->topic->title }} нийт {{ $duplicate->total }} асуулт байна.<br>
            <input type="number" name="number[{{ $duplicate->topic->id }}]"></input><br><br>
            @endforeach
            <br/>

            </div>
        </div> 

2 个答案:

答案 0 :(得分:1)

Count首先收集对象数组,然后使用chunk()

$chunks =  $questions->chunk(ceil(($questions->count())/5));
foreach($chunks as $chunk){
      //dd($chunk)  //here $chunk is your parted collection
      //to get the individual data of parted collections, make another foreach
      foreach($chunk as $q){
           //$q is a individual object
      }
}

我如何$questions拆分为5 $collection:在chunk()方法中使用此

ceil(($questions->count())/5)

答案 1 :(得分:0)

您可以使用->toAttay()创建集合列表数组,然后就可以分块数据。