【问题标题】:Where can I put paginate()?我可以把分页()放在哪里?
【发布时间】:2020-09-17 12:24:30
【问题描述】:

我正在尝试将paginate(10) 稍后添加一个分页,但是当我添加它时,它给了我这个错误:

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$product

这是我要添加pagination(10)的控制器:

 public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);

        return view('your')->with('product', $user->product);
    }

我确实试过这个:

 public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id)->paginate(10);

        return view('your')->with('product', $user->product);
    }

如何避免未定义属性错误?

【问题讨论】:

    标签: ajax laravel laravel-5 bootstrap-4 laravel-4


    【解决方案1】:

    实际上find 函数仅用于获取 1 个结果,并且您正在对导致错误的结果应用分页。

    您可能希望对产品数据进行分页,这样就可以了

    public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id)->product()->paginate(10);
    
        return view('your')->with('product', $user);
     }
    

    【讨论】:

    • 非常感谢
    • @seique3 我的快乐兄弟 :),你能支持我的回答吗?
    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 2012-10-14
    • 2013-09-21
    • 2020-09-30
    • 2016-08-25
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多