Yousef Altaf
Yousef Altaf

Reputation: 2763

using Pagination with orderBy Eloquent Model

I have a function witch is getting all properties joining the images tables with orderBy and need to add paginate

here is my function

public function show_all()
    {
        $properties = PropertyDetail::where('active', 1)->with('propImages')->paginate(15)
            ->orderBy('id', 'DESC')
            ->get();
        return View::make('portal.properties.view_all', compact('properties'));
    }

in my view I got

call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'orderBy'

another thing when I removed the orderBy('id', 'DESC')->get() and I try it works but when I try to put {{ $prop->links() }} in the view I got

Call to undefined method Illuminate\Database\Query\Builder::links()

here is how my view looks like

    @foreach($properties as $prop)
         <div class="property col-xs-12 col-sm-6 col-md-4">
             <div class="image">
                 <div class="content imgContainer">
                      {{ HTML::link('property-details/'.$prop->id, '') }}
                            @foreach($prop->propImages->slice(0, 1) as $image)
                                       {{ HTML::image('images/propertyImages/'.$image->image, $prop->title) }}
                            @endforeach
                 </div>

                 <div class="price"> OMR. <span class="priceNumber"> {{ $prop->price }}</span></div>
              </div>

              <div class="title">
                   <h2>{{ HTML::link('', $prop->title, array('title'=>'$prop->title')) }}</h2>
              </div>

              <div class="location">{{ trans('location.'.$prop->propLocation->city) }}</div>

                  <div class="bathrooms">
                       <div class="content">{{ $prop->bathroom }}</div>
                  </div>
              <div class="bedrooms">
                   <div class="content">{{ $prop->bedroom }}</div>
              </div><!-- /.bedrooms -->
              <div class="receptionRoom">
              <div class="content">{{ $prop->dining_room }}</div>
                         </div>
                             </div>
             @endforeach

{{ $prop->links() }}

Upvotes: 1

Views: 259

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

The correct syntax is:

$properties = PropertyDetail::where('active', 1)
                  ->with('propImages')
                  ->orderBy('id', 'desc')
                  ->paginate(15);

Upvotes: 4

Related Questions