【问题标题】:Laravel Livewire Pagination Not Displaying Search Results After Index PageLaravel Livewire 分页在索引页面后不显示搜索结果
【发布时间】:2020-12-18 20:30:37
【问题描述】:

我看过其他关于这个问题的帖子,但没有一个反映我的设置,这让我很难实施。

我正在使用 Laravel 8.0 和 Livewire 1.3

我目前有一个包含 186 个项目的“项目”列表,每页 25 个分页 - 这很好用。我也有实时搜索,如果在索引页面上,效果很好,但如果您在索引后的任何页面上,例如 http://workorders.test/items?page=2 并决定进行搜索,它将显示正确数量的结果,但不显示结果到页面(见附件截图)。

index.php

<?php

namespace App\Http\Livewire\Admin\Items;

use Livewire\Component;
use App\Item;
use Livewire\WithPagination;

class Index extends Component
{
    use withPagination;
    public $item;
    public $item_name;
    public $item_description;
    public $perPage = 25;
    public $sortAsc = true;
    public $search = '';
    public $sortField = 'item_name';

    ...

    public function render()
    {
        $types = collect([
            ['name' => 'Equipment'],
            ['name' => 'Supply'],
        ]);

        $plucked = $types->pluck('name')->all();

        $items = Item::search($this->search)
            ->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
            ->paginate($this->perPage);
        return view('livewire.admin.items.index', compact('items', 'plucked'));
    }
}

item.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Item extends Model
{
    use SoftDeletes;
    protected $fillable = [
        'item_name', 'item_description'
    ];

    public function products()
    {
        return $this->hasMany(Product::class);
    }

    public static function search($query)
    {
        return empty($query) ? static::query()
            : static::where('item_name', 'like', '%'.$query.'%');
    }

}

index.blade (仅显示重要部分)

<div class="flex-1 min-w-0 rounded-md shadow-sm">
   <input wire:model="search" id="search" placeholder="Search Item Name">
</div>
...
@foreach($items as $item)
   ... 
@endforeach
...
{{ $items->links() }}

【问题讨论】:

    标签: php laravel laravel-livewire


    【解决方案1】:

    你错过了一些东西。当你使用这样的过滤器时,你需要告诉 livewire 重置页面。

    在您的情况下,过滤器存储在 $search 变量中,因此只需将此方法添加到您的 Livewire 组件:

    public function updatingSearch()
    {
        $this->resetPage();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 2011-08-25
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多