erm_durr
erm_durr

Reputation: 537

Why generating pagination links raises 'Call to undefined method'?

I've got an problem. This is my first time using pagination.

This is my view:

@extends('base')
@section('title', 'News')
@stop
@section('main')

@if ($news->count())
@foreach($news as $news)
<div class="container">
    <div class="doc-content-box">
        <legend>{{ $news->title }} <div id="spaceholder"></div>

            <p>
                <h6><span class="muted"><i class="icon-calendar"></i> {{ $news->created_at }} &nbsp;&nbsp;&nbsp;&nbsp;<a href="/profile/{{ $news->author }}">{{ $news->author }}</a><span>
                </p>
            </h6>
        </legend>
        <div style="margin-top:7px">

            <p>{{ $news->content }}</p>
        </div>



        <div class="btn-group">
            <a class="btn btn-primary" href="{{ URL::route('news.edit', $news->id) }}"><i class="icon-pencil"></i> Edit</a>

            <a class="btn btn-danger" href="{{ URL::route('news.destroy', $news->id) }}"><i class="icon-trash icon-large"></i> Delete</a>
        </div>


    </div> <!-- /container -->
    <div id="spaceholder">&nbsp;</div>
@endforeach

{{ $news->links() }}

@else
<div class="container">
    <div class="doc-content-box">
        <legend>News</legend>
    <p>There are no news.<br /><br />If you wish to create a news, you should go to our <a href="{{ URL::to('news') }}">news management page!</a></p>
</div>
</div>
@endif

@stop

Then it results into an error Call to undefined method Illuminate\Database\Query\Builder::links().

I've also tried changing @foreach($news as $news) to @foreach($news->results as $news), but then I get an new error:

Undefined property: Illuminate\Pagination\Paginator::$results

The paginator itself works, but the pagination links (next and previous) won't work.

Upvotes: 0

Views: 4373

Answers (2)

j1mbo69
j1mbo69

Reputation: 1

If you test that code :

@foreach($news as $new)
    <div class="container">
        <div class="doc-content-box">
            <legend>{{ $new->title }} <div id="spaceholder"></div>

                <p>
                    <h6><span class="muted"><i class="icon-calendar"></i> {{ $new->created_at }} &nbsp;&nbsp;&nbsp;&nbsp;<a href="/profile/{{ $new->author }}">{{ $new->author }}</a><span>
                    </p>
                </h6>
            </legend>
            <div style="margin-top:7px">

                <p>{{ $new->content }}</p>
            </div>



            <div class="btn-group">
                <a class="btn btn-primary" href="{{ URL::route('news.edit', $new->id) }}"><i class="icon-pencil"></i> Edit</a>

                <a class="btn btn-danger" href="{{ URL::route('news.destroy', $new->id) }}"><i class="icon-trash icon-large"></i> Delete</a>
            </div>


        </div> <!-- /container -->
        <div id="spaceholder">&nbsp;</div>
    @endforeach

Upvotes: 0

Rubens Mariuzzo
Rubens Mariuzzo

Reputation: 29231

This happens when your query has not been executed. Your variable $news is a query builder not an array of objects. Before calling:

$news->links();

You need to execute your query by calling:

$news = $news->get();

Then after calling the get() method it will return an array representing the result set instead of an instance of Illuminate\Database\Query\Builder.


Note: when you do a foreach in a instance of Illuminate\Database\Query\Builder it will execute the query but the results will be scoped inside the foreach loop. That's the reason that you cannot retrieve the pagination links, because the object you are referencing is the query instead of the results.

Upvotes: 1

Related Questions