将项目添加到分页器laravel 4


Add items to paginator laravel 4

所以当我想向分页器集合添加元素时遇到了问题,我得到了这个错误:

call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate'Support'Collection' does not have a method 'add'

这是我的代码:

$data = $users->whereBetween('program_date',[$from,$to . ' 23:59:59'])->paginate(10);
$tests = $test->whereBetween('created_at',[$from,$to . ' 23:59:59'])->paginate(10);
foreach ($tests as $sms) {
    $camp = new Campagne();
    $camp->user_id = $sms->user_id;
    $data->add($camp); // i got error in this line!!
}

因此,如果有人知道如何在laravel分页中使用->add($element),我将非常感激。

我不认为Collection有一个叫做add的函数。我也不确定你是否可以直接从Paginator循环通过,至少不能循环通过模型。我认为你需要把它改成
foreach( $tests->getItems() as $sms ){
  .....
}

它不能以这种方式工作的原因是,当你对集合进行分页时,你会得到Illuminate''Pagination''Paginator类的一个实例,当你循环通过时,你基本上是在循环类对象,而不是你的项目。