setPath()在Laravel

时间:2018-12-09 08:22:14

标签: php laravel laravel-pagination

我正在使用LengthAwarePaginator进行分页。但是它没有保存排序的值。

如果排序值为1,则应按升序对名称进行排序。它给我结果第一页。但是,当我单击第二个。这不是结果。

我的路线是

Route::get('/boat-list/{drop_id}', 'CommonController@sortDrop');

这是我的控制人

public function sortDrop(Request $request){
    $sort = $request->sort;

    $boat_list = new Boats;

    $current_page = LengthAwarePaginator::resolveCurrentPage();

    //$boat_city = DB::select("SELECT c.name FROM `boats` AS b, `city` AS c WHERE b.city = c.id");

    if($sort == 1){
        $boat_list = DB::select("SELECT b.id, b.image, b.boat_name, b.rate, b.year, b.type, c.name AS city FROM boats AS b, city AS c WHERE b.city = c.id ORDER BY b.rate ASC");
        $current_page_orders = array_slice($boat_list, ($current_page - 1) * 5, 5);
        $boat = new LengthAwarePaginator($current_page_orders, count($boat_list), 5);
        $boat->setPath('/boat-list');
        return view('user-interface/sort_by', compact('boat'));
    } else if($sort == 2){
        //$boat = $boat_list::orderBy('rate', 'DESC')->paginate(5);
        $boat_list = DB::select("SELECT b.id, b.image, b.boat_name, b.rate, b.year, b.type, c.name AS city FROM boats AS b, city AS c WHERE b.city = c.id ORDER BY b.rate DESC");
        $current_page_orders = array_slice($boat_list, ($current_page - 1) * 5, 5);
        $boat = new LengthAwarePaginator($current_page_orders, count($boat_list), 5);
        $boat->setPath('/boat-list');
        return view('user-interface/sort_by', compact('boat'));
        //return view('user-interface/sort_by', compact('boat', 'boat_city'));
    } else if($sort == 3){
        //$boat = $boat_list::orderBy('boat_name', 'ASC')->paginate(5);
        //return view('user-interface/sort_by', compact('boat', 'boat_city'));
        $boat_list = DB::select("SELECT b.id, b.image, b.boat_name, b.rate, b.year, b.type, c.name AS city FROM boats AS b, city AS c WHERE b.city = c.id ORDER BY b.name ASC");
        $current_page_orders = array_slice($boat_list, ($current_page - 1) * 5, 5);
        $boat = new LengthAwarePaginator($current_page_orders, count($boat_list), 5);
        $boat->setPath('/boat-list');
        return view('user-interface/sort_by', compact('boat'));
    } else if($sort == 4){
        //$boat = $boat_list::orderBy('boat_name', 'DESC')->paginate(5);
        //return view('user-interface/sort_by', compact('boat', 'boat_city'));
        $boat_list = DB::select("SELECT b.id, b.image, b.boat_name, b.rate, b.year, b.type, c.name AS city FROM boats AS b, city AS c WHERE b.city = c.id ORDER BY b.name DESC");
        $current_page_orders = array_slice($boat_list, ($current_page - 1) * 5, 5);
        $boat = new LengthAwarePaginator($current_page_orders, count($boat_list), 5);
        $boat->setPath('/boat-list');
        return view('user-interface/sort_by', compact('boat'));
    } else if($sort == 0){
        //$boat = $boat_list::paginate(5);
        //return view('user-interface/sort_by', compact('boat', 'boat_city'));
        $boat_list = DB::select("SELECT b.id, b.image, b.boat_name, b.rate, b.year, b.type, c.name AS city FROM boats AS b, city AS c WHERE b.city = c.id");
        $current_page_orders = array_slice($boat_list, ($current_page - 1) * 5, 5);
        $boat = new LengthAwarePaginator($current_page_orders, count($boat_list), 5);
        $boat->setPath('/boat-list');
        return view('user-interface/sort_by', compact('boat'));
    }
}

我的观点是

 @extends('layouts.app')
@section('webtitle', 'BoardMyBoat | Home')
@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">
<body>
    @include('layouts.header')

    <!--=================================
        inner-intro -->
    <section class="inner-intro bg-1 bg-overlay-black-70">
        <div class="container">
            <div class="row text-center intro-title">
                <div class="col-md-6 text-md-left d-inline-block">
                    <h1 class="text-white">Boat Listing</h1>
                </div>
                <div class="col-md-6 text-md-right float-right">
                    <ul class="page-breadcrumb">
                        <li><a href="#"><i class="fa fa-home"></i> Home</a> <i class="fa fa-angle-double-right"></i></li>
                        <li><span>Boat Listing</span> </li>
                    </ul>
                </div>
            </div>
        </div>
    </section>
    <!--=================================
        inner-intro -->

    <?php
        $bcount = count($boat);
        $ycount = count($year);
        $tcount = count($type);
        $pcount = count($price);
        $ccount = count($city);
    ?>

    <!--=================================
        product-listing  -->
    <section class="product-listing page-section-ptb">
        <input type="hidden" name="_token" value="{{ csrf_token() }}" id="_token"/>
        <div class="container">
            <div class="row">
                <div class="col-lg-3 col-md-4">
                    <div class="listing-sidebar">
                        <div class="widget">
                            <div class="widget-search">
                                <h5>Advanced Search</h5>
                                <ul class="list-style-none">
                                    <li><i class="fa fa-star"> </i> Results Found <span class="float-right">({{$pcount}})</span></li>
                                    <li>
                                        <a class="button red searchBtn" href="#" id="searchBtn">Search</a>
                                    </li>
                                </ul>
                            </div>
                            <div class="clearfix">
                                <ul class="list-group">
                                    <!--li class="list-group-item">
                                        <a href="#">Year</a>
                                        <ul>
                                            <li>
                                                <span class="checkbox">
                                                    <label>
                                                        <input type="checkbox" id="yearAll"> All Years
                                                    </label>
                                                </span>
                                            </li>
                                            @if($ycount>0)
                                                @for($i=0; $i<$ycount; $i++)
                                                    <li>
                                                        <span class="checkbox">
                                                            <label>
                                                                <input type="checkbox" name="year[]" value="{{$year[$i]->year}}" class="year_drop"> {{$year[$i]->year}}
                                                            </label>
                                                        </span>
                                                    </li>
                                                @endfor
                                            @endif
                                        </ul>
                                    </li>
                                    <li class="list-group-item">
                                        <a href="#">Model</a>
                                        <ul>
                                            <li>
                                                <span class="checkbox">
                                                    <label>
                                                        <input type="checkbox" id="typeAll"> All Models
                                                    </label>
                                                </span>
                                            </li>
                                            @if($tcount>0)
                                                @for($i=0; $i<$tcount; $i++)
                                                    <li>
                                                        <span class="checkbox">
                                                            <label>
                                                                <input type="checkbox" name="type[]" value="{{$type[$i]->type}}" class="type_drop"> {{$type[$i]->type}}
                                                            </label>
                                                        </span>
                                                    </li>
                                                @endfor
                                            @endif
                                        </ul>
                                    </li-->
                                    <li class="list-group-item">
                                        <a href="#">City</a>
                                        <ul>
                                            <li>
                                                <span class="checkbox">
                                                    <label>
                                                        <input type="checkbox" id="cityAll"> All City
                                                    </label>
                                                </span>
                                            </li>
                                            @if($ccount>0)
                                                @for($i=0; $i<$ccount; $i++)
                                                    <li>
                                                        <span class="checkbox">
                                                            <label>
                                                                <input type="checkbox" name="city" value="{{$city[$i]->id}}" class="city_drop"> {{$city[$i]->name}}
                                                            </label>
                                                        </span>
                                                    </li>
                                                @endfor
                                            @endif
                                        </ul>
                                    </li>
                                    <li class="list-group-item">
                                        <a href="#">Price Range</a>
                                        <ul>
                                            <div class="price-slide">
                                                <div class="price">
                                                    <input type="text" id="amount" class="amount" value="@if($pcount>0)NZD {{$price[0]->min}} - NZD {{$price[0]->max}}@endif"/>
                                                    <div id="slider-range"></div>
                                                    <input type="hidden" name="min_price" value="{{ $price[0]->min }}" id="min_price"/>
                                                    <input type="hidden" name="max_price" value="{{ $price[0]->max }}" id="max_price"/>
                                                </div>
                                            </div>
                                        </ul>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-9 col-md-8">
                    <div class="sorting-options-main">
                        <div class="row">
                            <div class="col-md-4 text-right">
                                <div class="selected-box">
                                    <select id="sort">
                                        <option value="0">Sort by </option>
                                        <option value="1">Price: Lowest first</option>
                                        <option value="2">Price: Highest first </option>
                                        <option value="3">Boat Name: A to Z </option>
                                        <option value="4">Boat Name: Z to A </option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <a class="button red searchBtn" id="sortBtn" href="">Sort</a>
                            </div>
                        </div> 
                    </div>
                    <div id="sort_id">
                        @if($bcount>0)
                            @for($i=0; $i<$bcount; $i++)
                                <div class="car-grid">
                                    <div class="row">
                                        <div class="col-lg-4 col-md-12">
                                            <div class="car-item gray-bg text-center">
                                                <div class="car-image">
                                                    <img class="img-fluid" src="{{$boat[$i]->image}}" alt="">
                                                    <div class="car-overlay-banner">
                                                        <ul> 
                                                            <li><a href="#"><i class="fa fa-link"></i></a></li>
                                                            <li><a href="#"><i class="fa fa-shopping-cart"></i></a></li>
                                                        </ul>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-lg-8 col-md-12">
                                            <div class="car-details">
                                                <div class="car-title">
                                                    <a href="#">{{$boat[$i]->boat_name}}</a>
                                                    <p></p>
                                                </div>
                                                <div class="price">                                             <span class="new-price">NZD {{$boat[$i]->rate}}/Hr</span>
                                                    <a class="button red float-right" href="{{env('APP_CONSTANT_URL')}}myboats/{{$boat[$i]->id}}">Details</a>
                                                </div>
                                                <div class="car-list">
                                                    <ul class="list-inline">
                                                        <li><i class="fa fa-location-arrow"></i> {{$boat[$i]->city}} </li>
                                                        <li><i class="fa fa-history"></i> {{$boat[$i]->year}} </li>
                                                        <li><i class="fa fa-cog"></i> {{$boat[$i]->type}} </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            @endfor
                        @endif
                        <div class="pagination-nav d-flex justify-content-center">
                            <ul class="pagination">
                                {{$boat->render()}}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--=================================
        product-listing  -->



    @include('layouts.footer')
    @include('layouts.scriptfooter')
    <script type="text/javascript">
        POTENZA.priceslider = function () {
           if($(".price-slide").exists()) {
                $("#slider-range").slider({
                    range: true,
                    min: <?php echo $price[0]->min; ?>,
                    max: <?php echo $price[0]->max; ?>,
                    values: [<?php echo $price[0]->min; ?>, <?php echo $price[0]->max; ?>],
                    slide: function(event, ui) {
                        var min = ui.values[0],
                            max = ui.values[1];
                          $('#' + this.id).prev().val("NZD " + min + " - NZD " + max);
                          $('#min_price').val(min);
                          $('#max_price').val(max);
                    }
                });
            }
        }
    </script>
</body>
@endsection

您能帮我解决这个问题吗?

0 个答案:

没有答案