【问题标题】:Laravel Livewire Pagination Result Disappear After Clicking Next Page/Page NumberLaravel Livewire 分页结果在单击下一页/页码后消失
【发布时间】:2023-03-05 02:31:01
【问题描述】:

我一直在寻找类似上述问题的问题,但我找不到,所以我会做一个

我的问题是,当我用 Laravel Livewire 做一个非常简单的分页时,第一页看起来很好,但是当我点击“下一页/页码”时,结果就消失了,即使还有更多的结果秀,我完全不知道,无论我如何尝试。

As you can see, the simple pagination work just fine, there is next and previous button and a very big next and previous picture below

But the moment i click next page or page number, the result just disappear, the total number of result even go to 0

这是我的组件代码:

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Http\Request;
use DB;
use Livewire\WithPagination;

class Category extends Component
{   
    use WithPagination;
    public function render(Request $request)
    {   
        $id = $request->id;

        $product_count = DB::table('product')->where('category_id', $id)->count();

        $page = $request->page ?? 1;

        $product_all = DB::table('product')->where('category_id', $id)->paginate(3);

        return view('livewire.category',[
            'product_all' => $product_all,
            'product_count' => $product_count,
            'page' => $page,
        ]);
    }
}

这是我的查看代码:

<div class="row">
    @foreach ($product_all as $product)
    <div class="col-lg-4 col-md-6 col-12 mt-4 pt-2">
        @php
            product($product);
        @endphp
    </div>
    @endforeach   

    <!-- PAGINATION START -->
    @if ($product_all instanceof \Illuminate\Pagination\LengthAwarePaginator)
    {{ $product_all->links() }}
    @endif
    <!-- PAGINATION END -->
</div>

还有一个小提示,我尝试在没有 livewire 的情况下进行分页(只是正常的 laravel 分页),它工作得非常好,这就是为什么我真的对正在发生的事情一无所知

我已经包含了 livewire @livewireStyles 和 @livewireScripts,我在其他任何地方都找不到答案,因为我没有看到任何与我的问题相匹配的问题,而且我对 livewire 有点陌生

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    我认为您的问题与livewire组件的render方法上的request参数有关,一旦rerender上的livewire无法访问请求值,直到下一次浏览器刷新。

    public function render(Request $request)
    

    相反,使用 livewire 属性将 id 和页面绑定到 livewire 可以在重新渲染时读取的值

    @livewire('some-component', ['category_id' => $parent_category_id, 'page' => $parent_page_value])
    

    与上面一样,livewire 将此值绑定到自身属性并在重新渲染时读取它们

    public $category_id, $page;
    
    public function render()
    {
       $product_count = DB::table('product')->where('category_id', $this->category_id)->count();
       $product_all = DB::table('product')->where('category_id', $this->category_id)->paginate(3);
    
       return view('livewire.category',[
          'product_all' => $product_all,
          'product_count' => $product_count,
          'page' => $this->page,
       ]);
    }
    

    【讨论】:

    • 谢谢,你解决了我 9 小时只看显示器不知道怎么做的问题,非常感谢
    • 很高兴知道。那就去吧,恢复所有的时间! jjj
    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 2021-01-21
    • 1970-01-01
    • 2021-02-10
    • 2019-10-03
    • 2021-05-01
    • 2013-10-29
    • 1970-01-01
    相关资源
    最近更新 更多