Laravel - Paginate Random records

前端 未结 3 1940
北荒
北荒 2021-01-05 14:19

How we can paginate through random records in laravel? for example:

$products = Product::all()->orderBy(DB::raw(\'RAND()\'));
$products->paginate(4);
$         


        
3条回答
  •  独厮守ぢ
    2021-01-05 15:12

    Solution Ajax + chunk + session + lazyload + paginate + eloquent + blade

    get all products, set the number of item for each chunk (like paginate) and store it into session (we store the chunks into session so when ajax request comes in it won't call $products = Product::inRandomOrder()->get(); again) , first chunk will be the first pagination

    when ajax is requesting for more products it will get the products from session and get the correct chunk of products base on the page number ajax requested.

    if there are no more chunks just return without any products

        if(!$request->ajax()){
    
            $products = Product::inRandomOrder()->get();
            $chunks = $products->chunk(4);
    
            \Session::put('chunks',$chunks);
            $products = $chunks[0];
        }else{
            $page = $request->all()['page'];                
            $chunks = \Session::get('chunks');
    
            if(count($chunks)<$page){
                \Session::forget('chunks');
                return;
            }else{
                $products = $chunks[$page-1];
            }
        }
    
       if ($request->ajax()) {
            $view =  view('product_lazyLoad',compact('products'))->render();
            return response()->json(['html'=>$view]);
       }
    
       return view('products',compact('products'));
    

    products.blade.php : main page

    @if($products)
        
      @include('product_lazyLoad')
    @endif

    product_lazyload.blade.php : display products

    @foreach($products as $product)
        

    Display your products here

    @endforeach

    font-end ajax call : when the page is scroll to bottom of the page the variable page will auto increment and request backend for more products. if more products are return it will append the products to element with id = "productlist" in products.blade.php

        //lazy load
        var page = 1;
        $(window).scroll(function() {
            if($(window).scrollTop() + $(window).height() >= $(document).height()) 
            {
                page++;
                loadMoreData(page);
            }
        });
    
      function loadMoreData(page){
            $.ajax({
                url: '?page=' + page,
                type: "get",
                beforeSend: function(){
                    $('.ajax-load').show();
                }
            }).done(function(data){
                if(!data.html){
                    $('.ajax-load').html("No more records found");
                    $('.ajax-load').show();
                    return;
                }
    
                $('.ajax-load').hide();
                $("#productLists").append(data.html);
    
            }).fail(function(jqXHR, ajaxOptions, thrownError){
                 //error handle
            });
        }
    

提交回复
热议问题