dpbz14739 2018-09-08 20:03
浏览 84
已采纳

来自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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误