分页在laravel中无法正常工作

时间:2016-01-27 08:38:30

标签: php laravel-5

我的分页工作不正常。当点击分页时,显示结果需要一些时间,当我们一个接一个地点击页面时,页面会不断加载。为什么会发生这种情况......任何人都可以帮我这个????? .............

我的view.blade.php

<div class="carousel-inner" role="listbox">    
                                  <div class="item active"> <img src="{{ Config::get('constants.site_url') }}public/hotel_images/{{ $rat->image }}" class="img-responsive center-object"/> </div>
                                  @if(!empty($rat->imagestore))
                                    @foreach($rat->imagestore as $image)

                                        <div class="item"> <img src="{{ Config::get('constants.site_url') }}public/uploadStoreImages/{{ $image->store_image }}" class="img-responsive center-object"/> </div>
                                    @endforeach
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="col-lg-6">
                            <p>{{$rat->restaurant}}</p>
                            <p>Address : {{$rat->address}}  </p>
                            <p>City : {{$rat->city}} </p>
                            <p>Zip : {{$rat->zipcode}}</p>
                            <p>Offer : {{$rat->offer}} </p>
                            <p>Price : $$  </p>
                            <!--<p>Price : {{$rat->price}} </p>-->
                            <p>Link : <a href="{{$rat->url}}">{{$rat->url}}</a> </p>                            
                            <p>{{$rat->description}} </p>

                        </div>

                        <div class="col-lg-3">
                            <h3>{{$rat->phone}}</h3>
                            <a href="#" class="special" data-toggle="modal" data-target="#myModal_{{$rat->id}}" data_value="{{ $rat->id }}">Special</a>
                        </div>
                    </div>
                </div>
              </div>

pagination.blade.php页面

<div class="posts">

@include('front_end/stores/stores1')
</div>


 <script>

     /* var url = window.location.href; 
      var parts = url.split("?");
      var result = parts[parts.length - 1]; */

    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getPosts(page);
            }
        }
    });
    $(document).ready(function() {
        $(document).on('click', '.pagination a', function (e) {
            getPosts($(this).attr('href').split('page=')[1]);
            e.preventDefault();
        });
    });
    function getPosts(page) {


      var url = window.location.href;   
      var parts = url.split("?");
      var result = parts[parts.length - 1];

      $.ajax({
            url : '?page=' + page ,//'&cat_id={{ $cat_id }}',
         }).done(function (data) {
            //console.log(data);
            $('.posts').html(data);
            location.hash = page;
        }).fail(function () {
            alert('Posts could not be loaded.');
        });
    }
    </script>

Controller.php这样

if($cat_id != 0){
            if($subcat_id == 0){
                $restaurant = Restaurant::leftjoin('cities','restaurant.city','=','cities.id')
                             ->leftjoin('cuisine','restaurant.cuisine','=','cuisine.id')
                             ->where('restaurant.category','=',$cat_id)
                             ->select('*','restaurant.id as id','restaurant.id as rest_id','restaurant.created_at as user_created')->orderBy('restaurant.id','desc')
                             ->paginate(3);
            }else{
                $restaurant = Restaurant::leftjoin('cities','restaurant.city','=','cities.id')
                             ->leftjoin('cuisine','restaurant.cuisine','=','cuisine.id')
                             ->where('restaurant.category','=',$cat_id)
                             ->where('restaurant.subcategory','=',$subcat_id)
                             ->select('*','restaurant.id as id','restaurant.id as rest_id','restaurant.created_at as user_created')->orderBy('restaurant.id','desc')
                             ->paginate(3);
            }

        }

0 个答案:

没有答案