Laravel 5 LengthAwarePaginator在页面上显示原始HTML

时间:2015-02-21 20:13:46

标签: laravel-5

我试图用Laravel实现分页,我的分页链接显示为网页上的原始html。我对Laravel来说相当新,而且我不确定自己哪里出错了。我正在使用Laravel 5。

UserController中@指数:

public function index()
    {
        $page = \Input::get('page', 1);
        $data = $this->user->getByPage($page, 10);

        $users = Pagination::makeLengthAware($data->items, $data->totalItems, 10 );

        return view('admin.users.index', compact('users'));
    }

EloquentUser getByPage方法:

public function getByPage($page=1, $limit=10)
    {
        $result = new \StdClass;
        $result->page = $page;
        $result->limit = $limit;
        $result->totalItems = 0;
        $result->items = [];

        $users = $this->user->skip($limit * ($page - 1))
                       ->take($limit)
                       ->get();

        $result->totalItems = $this->user->count();
        $result->items = $users->all();

        return $result;
    }

分页服务:

<?php namespace App\Services;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

abstract class Pagination
{
    /**
     * Create paginator
     *
     * @param object $data Object containing the paginated data
     * @param int $total
     * @param int $perPage
     * @return string
     */
    public static function makeLengthAware($data, $total, $perPage)
    {
        $paginator = new LengthAwarePaginator(
            $data,
            $total,
            $perPage,
            Paginator::resolveCurrentPage(),
            ['path' => Paginator::resolveCurrentPath()]);

        return $paginator;
    }
}

admin.users.index查看:

@foreach($users as $user)
   <tr>
       <td>{{ $user->id }}</td>
       <td>{{ $user->username }}</td>
       <td>{{ $user->email }}</td>
       <td>Role</td>
       <td>{{ $user->created_at }}</td>
   </tr>   
 @endforeach

{{ $users->render() }}

数据按预期显示在表格中,但$users->render()显示页面上的原始html。

任何人都知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:5)

在Laravel 5中,您必须使用{!! !!}作为原始HTML输出。否则它将被逃脱。

{!! $users->render() !!}

请查看this answer以了解刀片回送标记与如何更改它们的说明之间的比较。