当尝试更改页面时,分页用旧的搜索结果替换新的搜索结果

时间:2016-08-14 19:23:29

标签: php pagination laravel-5.2

我在app的主页上添加了一个ajax搜索框。它工作正常。但是当我在新结果出现后使用分页时,分页会让我看到旧的结果。我不确定我做得不对。所以寻找专家的帮助来获得解决方案。

我在routes -

中有这些
Route::get('/', 'BookmarkController@index');
Route::post('/', 'BookmarkController@search');

我在他们的控制器中有这些 -

public function index(Request $request)
{
    $tags_list = Tag::orderBy('tag', 'asc')->get();
    $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->paginate(10);
    $bookmarks_all = Bookmark::orderBy('created_at','desc')->where('public', '1')->get();
    return view('welcome')->with('bookmark', $bookmarks)->with('tags_list', $tags_list)->with('bookmarks_all', $bookmarks_all);
}

public function search(Request $request){
    $search_value = $_POST['search'];

    $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);
    return view('public_bookmarks')->with('bookmark', $bookmarks);
}

welcome.blade.php

<div class="container content-container">
<div class="row ">
    <div class="col-sm-12">
        <div class="page-header">
            <div class="row">
                <span class="title col-sm-8">Recent Bookmarks</span>
                <form id="demo-2" class="search-form col-sm-4" method="post">
                    {{ csrf_field() }}
                    <div class="input-group">
                        <!-- <input id="search" class="form-control" onkeyup="search_data(this.value, 'result');" placeholder="Search" name="search" value="" type="text"> -->
                        <input id="search" class="form-control" placeholder="Search" name="search" value="" type="text">
                        <span class="input-group-btn">
                            <button class="btn btn-primary" id="search-btn" type="button"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                    <script type="text/javascript">
                        $(document).ready(function() {
                            $('#search-btn').click(function(){
                                $.ajax({
                                    url: '/',
                                    type: "post",
                                    data: {'search':$('input[name=search]').val(), '_token': $('input[name=_token]').val()},
                                    success: function(data){
                                        $('#search-results').html(data);
                                    },

                                    error: function (data) {
                                        console.log('Error on Article extracting');
                                        console.log(data);
                                    }
                                }); 
                            });
                        });
                    </script>
                </form>
            </div>
        </div>
    </div>
    <div class="col-sm-9" id="search-results">

        @include ('public_bookmarks')
    </div>
</div>

public_bookmars.blade.php

@if (count($bookmark) > 0)
<div class="row card-row">
@foreach ($bookmark as $bookmark_single)
    <div class="col-sm-4 col-xs-12 card-parent" data-col="col-sm-4">
        <div class="card">
            <div class="card-part1">
                <div class="img-card">
                    <img src="{{$bookmark_single->thumbnail}}" />
                </div>
                <div class="card-content">
                    <h4 class="card-title">
                        {{ $bookmark_single->title }}
                    </h4>
                    <div class="card-desc">
                        {{ str_limit($bookmark_single->description, $limit = 50, $end = ' [...]') }}
                    </div>
                </div>
                <div class="card-read-more">
                    <p><?php   $tags = $bookmark_single->tags;
                        $tag_list = explode(',', $tags); ?>
                        @foreach ($tag_list as $tag)
                            <a href="/tag/{{$tag}}" class="label label-primary">{{$tag}}</a>
                        @endforeach
                    </p>
                    <p class="card-user">- &nbsp;<a href="/user/{{ $bookmark_single->bookmarker }}">{{ $bookmark_single->bookmarker }}</a></p>
                    <a class="v-link" target="_blank" href="{{ $bookmark_single->url }}">Visit the link</a>
                </div>
                <button type="button" class="btn btn-success btn-circle btn-lg btn-read-more"><i class="fa fa-chevron-right"></i></button>
            </div>

            <div class="card-part2 col-xs-0">
                {{ print $bookmark_single->contents }}
            </div>
        </div>
    </div>
@endforeach
</div>
{{ $bookmark->links() }}
@endif

1 个答案:

答案 0 :(得分:0)

您可以使用flash()方法来实现此目的。 Old Input
search()方法中添加$request->flash()。这将闪烁会话中的当前输入。 并且,下次您想通过$request->old('search');检索它 这是完整的search()方法:

public function search(Request $request){
    $search_value = $_POST['search']; //assign the current value of search field
    if(!$search_value) //check if current value is not null, means this is new search or previous one
{   
    $search_value = $request->old('search'); //If search_value is null use the old value
    $request->search = $request->old('search'); //add the old value to current request so that it can be flashed
}

    $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);

    $request->flash(); //adding this request's search value to the session
    return view('public_bookmarks')->with('bookmark', $bookmarks);
}

更新

public function search(Request $request){
    //$search_value = $_POST['search']; change this to use $request
    $search_value = $request->search;

    $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);

    return view('public_bookmarks')->with('bookmark', $bookmarks)->withInput($request->only('search')); // flashed input to the view
}

现在,在您的视图中,您可以使用old()方法获取值:

{{ $bookmark->appends(['search' => old('search')])->links()}}

更新2:

更改您的ajax调用以使用get方法并将其指向新地址:

type: "get",
url: '/search'

添加一个路径来处理搜索请求作为get方法:

Route::get('/search', 'BookmarkController@search');

search()方法中,使用$request获取搜索值:

$search_value = $request->search;