dpbz14739
2018-09-08 20:03
浏览 82

来自AJAX请求的分页无法正常工作 - Laravel 5.6

I'm using Laravel 5.6 -- Jquery Ajax

Here's the point

I'm having a search input placed in my navbar there's an eventlistener('keyup') on it.

Everytime keyup is fired, an AJAX GET request is send to

url : '{{action('PostController@searchAdmin')}}'

From web.php : Route::get('/search/admin', 'PostController@searchAdmin');

I made the return of that action a partial with data

return view('back.partials.searchResult', ['posts' => $posts, 'trashed' => $trashed]);

And I replace the content of the main tag with that partial

Everything is working properly except when the result count is greater than 10 (the breakpoint of pagination).

Pagination control links are all pointing to "search/admin?page=x" and when I click on it, this error is showing

Undefined variable: posts

I used $posts->links() to show the controls

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwendu2460 2018-09-11 08:18
    已采纳

    I found a solution so I post it

    In web.php

    Route::get('/search', function(Request $request) {
        $search = $request->search;
        $trashed = Post::trash()->count();
        $posts = Post::notTrash()
            ->where('title', 'like', '%' . $search . "%")
            ->orWhere('post_type' , 'like', '%' . $search . '%')
            ->paginate(10);
    
    
        $posts->withPath('?search=' . $search);
        return view('back.partials.searchResult', ['posts' => $posts, 'trashed' => $trashed, 'search' => $search]);
    });
    

      This code was for test purpose and will be soon exported in a new controller called SearchController

    In my PostController

    public function index(Request $request)
    {
    
        // GET parameters
        $paginate = $request->input('paginate') ?? 10;
        $search = $request->input('search') ?? null;
    
        if($search !== null) {
            $posts = $this->checkCategories($paginate, $search);
        } else {
            $posts = Post::notTrash()->orderBy('id', 'ASC')->paginate($paginate);
        }
        $trashed = Post::trash()->count();
        $posts->withPath('?search=' . $search);
    
        return view('back.index', ['posts' => $posts, 'trashed' => $trashed, 'search' => $search]);
    }
    

    Working with

    private function checkCategories($paginate, $search)
    {
        $categories = Category::all();
        foreach ($categories as $category) {
    
            if(strpos(strtolower($category->name), $search) === false) {
    // @TODO: Change for stripos 
                $posts = Post::notTrash()
                    ->where('title', 'like', '%' . $search . '%')
                    ->orWhere('post_type', 'like', '%' . $search . '%')
                    ->paginate($paginate);
            } else {
                return Category::find($category->id)->posts()->paginate($paginate);
            }
        }
    
        return $posts;
    
    }
    

    The index method now accept Request to handle get parameters when they are some.

    In my views

       @if($search !== null)
            {{ $posts->appends($search)->links() }}
        @else
            {{ $posts->links() }}
        @endif
    

    Now replace

    {{ $posts->links() }}
    

    The solution was $var->withPath() and handling GET parameters

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题