以下结果是'Undefined property:Illuminate \ Pagination \ Paginator :: $ username'错误。
应该怎么做?无论我看多少次,我似乎都会遇到一些错误(例如受保护的属性和非对象)?
AbsractRepository.php
/**
* Get Results by Page
*
* @param int $page
* @param int $limit
* @param array $with
* @return StdClass Object with $items and $totalItems for pagination
*/
public function getByPage($page = 1, $limit = 10, $with = array())
{
$result = new StdClass;
$result->page = $page;
$result->limit = $limit;
$result->totalItems = 0;
$result->items = array();
$query = $this->make($with);
$model = $query->skip($limit * ($page - 1))
->take($limit)
->get();
$result->totalItems = $this->model->count();
$result->items = $model->all();
return $result;
}
BaseAdminController.php
/**
* List resources
*
* @return void
*/
public function index()
{
$page = Input::get('page', 1);
$data = $this->model->getByPage($page, 10);
$objects = Paginator::make($data->items, $data->totalItems, 10);
$this->layout->content = View::make($this->namespace . '::admin.index')
->with('objects', compact('objects'));
}
index.blade.php
@foreach($objects as $user)
<tr class="">
<td>{{ $user->username }}</td>
<td>{{ $user->first_name }} {{ $user->last_name }}</td>
<td>{{ $user->email }}</td>
<td class="center">Super Administrator</td>
<td class="center">YES</td>
<td class="center"><a href="#"><i class="fa fa-pencil"></i></a></td>
</tr>
@endforeach
答案 0 :(得分:0)
问题可能在这里:
$model = $query->skip($limit * ($page - 1))
->take($limit)
->get();
$result->totalItems = $this->model->count();
$result->items = $model->all();
$model
您使用get()
,然后在分配到$result->items
时再次使用all()
方法。
应该在这里:
$result->items = $model;
答案 1 :(得分:0)
我似乎通过在调用视图时删除了压缩来解决了这个问题。例如,它现在代表:
/**
* List resources
*
* @return void
*/
public function index()
{
$page = Input::get('page', 1);
$data = $this->model->getByPage($page, 10);
$objects = Paginator::make($data->items, $data->totalItems, 10);
$this->layout->content = View::make($this->namespace . '::admin.index')
->withObjects($objects);
}
这意味着我现在可以正常运行foreach:
@foreach($objects as $user)
<tr class="">
<td>{{ $user->username }}</td>
<td>{{ $user->first_name }} {{ $user->last_name }}</td>
<td>{{ $user->email }}</td>
<td class="center">Super Administrator</td>
<td class="center">YES</td>
<td class="center"><a href="#"><i class="fa fa-pencil"></i></a></td>
</tr>
@endforeach
这似乎可以解决问题,一切都很好:)