点击分页时,Laravel 5.2新查询结果将恢复原状

时间:2016-08-15 15:33:19

标签: php pagination laravel-5.2

我在我的应用的索引页面中应用了搜索表单。当我搜索某些内容时,结果会更新。但是当我点击分页时,它会让我回到原来的结果。所以,我想我要更新分页或清除缓存或其他内容以及如何操作?

routes.php

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

BookmarkController.php

public function index(Request $request) {
    $tags_list = Tag::orderBy('tag', 'asc')->get();
    $bookmarks = Bookmark::orderBy('created_at', 'desc')->where('public', '1')->paginate(3);
    $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'];
    $tags_list = Tag::orderBy('tag', 'asc')->get();
    $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(3)
    ;

    $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)
        ->render()
    ;
}

welcome.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>
            <div class="clearfix text-right">
                {{!!$bookmark->render()!!}}
            </div>
        @endif

welcome.blade.php

中搜索表单HTML
<form id="demo-2" action="/" class="search-form col-sm-4" method="post">
    {{ csrf_field() }}
    <div class="input-group">
        <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="submit"><i class="fa fa-search"></i></button>
        </span>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

当您点击分页链接时,您会向同一页面发送get个请求,并最终使用BookmarkController@index

的第一条路线

此处的一种可能解决方案是将搜索路线更改为get而不是post,如:

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

并确保您的分页适用于此新路线,例如/ search?page = 2

如果您希望使用index方法保留搜索登录信息,则需要确保搜索过滤器不适用(如果它是空白的)

public function index(Request $request) {
    $tags_list = Tag::orderBy('tag', 'asc')->get();
    $bookmarks_all = Bookmark::orderBy('created_at','desc')->where('public', '1')->get();

    $search_value = $request->input('search', '');

    $bookmarks = Bookmark::where(function($query) use ($search_value) {
        if (!empty($search_value)) {
            $query->where('title', 'rlike', $search_value)
            ->orwhere('description', 'rlike', $search_value)
            ->orwhere('contents', 'rlike', $search_value)
            ->orwhere('tags', 'rlike', $search_value)
        }
    })->where('public', '1')->orderBy('created_at', 'desc')->paginate(3);

    return view('welcome')
        ->with('bookmark', $bookmarks)
        ->with('tags_list', $tags_list)
        ->with('bookmarks_all', $bookmarks_all)
    ;
}

并将搜索表单method更改为get