Joel R.
Joel R.

Reputation: 189

Laravel 5 LengthAwarePaginator Displaying Raw HTML on page

I'm trying to implement pagination with Laravel and my pagination links are being shown as the raw html on the webpage. I'm fairly new to Laravel and I'm not sure where I'm going wrong. I am using Laravel 5.

UserController@index:

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 method:

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;
    }

Pagination Service:

<?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 view:

@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() }}

The data shows up in the table as expected however the $users->render() displays the raw html on the page.

Anyone have any idea what I'm doing wrong here?

Upvotes: 0

Views: 4107

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152950

In Laravel 5 you have to use {!! !!} for raw HTML output. Otherwise it will be escaped.

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

Take a look at this answer for a comparison between the blade echo tags and an explanation on how to change them.

Upvotes: 5

Related Questions