0

Laravelでページネーションを機能させようとしています。以前にページネーションを使用したことがないため、ドキュメントとGoogleの結果からいくつかの例を使用しようとしています. 構成のどこかで「ページネーション」をオンにする必要があるかどうか疑問に思っています。

これが私の単純な作業コントローラーです:

public function get_index() {
  $locations = Location::all();
  return View::make('location.index')->with('locations', $locations)->render();
}

このようにページネーションを追加しようとすると、壊れます:

public function get_index() {
  $locations = Location::paginate(5);
  return View::make('location.index')->with('locations', $locations)->render();
}

..そして、エラーが発生します:

Unhandled Exception

Message:
Error rendering view: [location.index]

Trying to get property of non-object

これが私のlocation.indexビューです:

@layout('layouts.blank')
@section('content')
<div class="row">
<div class="twelve columns">
    <div role="main" id="content">
        <h3>Locations - All</h3>
        @if($locations)
            <table class="twelve">
                <thead>
                    <tr>
                        <th style="text-align: left;">Address</th>
                        <th>Area</th>
                        <th>Region</th>
                        <th>Edit</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                @foreach($locations as $location)
                <tbody> 
                    <tr>
                        <td style="text-align: left;">{{ $location->company,', ',$location->add_name,', ',$location->add_city }}</td>
                        <td> – </td>
                        <td> – </td>
                        <td>{{ HTML::link('location/update/'.$location->id, '',array('class'=>"foundicon-edit updateicon")) }}</td>
                        <td>{{ HTML::link('location/delete/'.$location->id, '',array('class'=>"foundicon-remove warnicon")) }}</td>
                    </tr>
                </tbody>
                @endforeach
            </table>
        @else
          Looks like we haven't added any locations, yet!
        @endif
        <p>{{ HTML::link('location/create', 'Create a location', array('class'=>"small radius button")) }}</p>
    </div>
</div>
</div>
@endsection
4

1 に答える 1

1

私はその主題を十分に深く読んでいませんでした。現在は機能しており、ビューで変更する必要があったのは次のとおりです。

//from:
@foreach($locations as $location)

//to:
@foreach($locations->results as $location)

それはたった5つの結果を返しました。次に、テーブルのフッターにリンクを追加するために、次の HTML/Blade を追加しました。

<tfoot>
  <tr>
    <td colspan="5"> {{ $locations->links() }} </td>
  </tr>
</tfoot>

ドキュメントでこれがひどく明確にわからなかったので、他の誰かが恩恵を受けることを願っています。

于 2013-04-04T15:47:43.807 に答える