【问题标题】:Laravel paginate redirect back, if page does not exist如果页面不存在,Laravel 分页重定向回来
【发布时间】:2018-07-20 23:29:15
【问题描述】:

我有情况。 我在愿望清单中有产品,我有行动:用 ajax “删除”。
我有分页:

$wishlists = Wishlist::with('product')->where('user_id', $profile->id)
             ->orderby('id', 'desc')->paginate(3);

最初我有 2 页关于分页。
当我从愿望清单中删除项目时,在分页中我有 1 页,但在页面上我有 2 页分页,因为我使用 ajax。如果用户在分页上不存在的页面上移动,我如何检查并在第一页分页上重定向他?

【问题讨论】:

    标签: javascript php ajax laravel pagination


    【解决方案1】:

    查询前可以查看总页数

    $totalRecords = Wishlist::with('product')->where('user_id', $profile->id)
                 ->count();
    
    $totalPage = $totalRecords/10; (where 10 is records per page)
    

    现在你检查

    $ajaxpage = 3;
    if($totalPage < $ajaxpage){
        $ajaxpage = 1;(get last page)
    }
    $wishlists = Wishlist::with('product')->where('user_id', $profile->id)
                 ->orderby('id', 'desc')->paginate($ajaxpage);
    

    【讨论】:

    • 不,这不起作用。如果页面分页不存在,我想在第一页重定向。
    • 比 $ajaxpage = 1;
    • 当我转到“site.com/se?page=3”时,结果为空,如何隐藏分页?
    猜你喜欢
    • 2017-03-12
    • 1970-01-01
    • 2021-07-08
    • 2014-02-13
    • 2014-06-12
    • 2014-02-13
    • 2016-04-04
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多