一次返回所有呼吸不会在生产中扩大。看 拉维尔(Laravel)强大的分页以提高性能。
功能版本中的原始代码看起来像:
<?php use App\Models\Chirp; use function Livewire\Volt\{state}; state(['chirps' => fn () => Chirp::with('user')->latest()->get()]); ?>
<div class="mt-6 bg-white shadow-sm rounded-lg divide-y">
@foreach ($chirps as $chirp)
<div class="p-6 flex space-x-2" wire:key="{{ $chirp->id }}">
...
</div>
@endforeach
</div>
阅读文档仅包含“类版本”的示例
Https://livewire.laravel.com/docs/pagination#basic-usage-类似:
use Livewire\WithPagination;
use Livewire\Volt\Component;
use App\Models\Post;
new class extends Component {
use WithPagination;
public function with(): array
{
return [
'posts' => Post::paginate(10),
];
}
}
因此,我已经尝试在视图中导入特征,例如:
use Livewire\WithPagination;
$getChirps = fn () => $this->chirps = Chirp::paginate(10);
or
$getChirps = function () {
return $this->chirps = Chirp::with('user')->paginate(15);
};
我会得到下一个错误:
livewire中不支持财产的Property类型: [{“ current_page”:1,“ data”:[{“ id”:1,“ user_id”:1,“ message”:“此chirp 是一个 测试“,” created_at”:“ 2025-02-14T03:24:24:24.000000Z”,“ Updated_at” “名称”:“ Roberto”,“电子邮件”:“
[电子邮件保护]”,“ email_verified_at”:null,“ create_at”:“ 2025-02-09T14:10:10:52.00 0000Z“,” UPDATED_AT”:“ 2025-02-09T14:10:52.000000Z”}}],“ first_page_url”:“ http://127.0.0.0.0.1:8000/chirps?page=1”,, “来自”:1,“ last_page”:1,“ last_page_url”:“ http://127.0.0.0.1:8000/chirps?page=1”,“ links”:[{“ url”:url“:null”:null,label“:bailel”: “«« 上一个”,“ active”:false},{“ url”:“ http://127.0.0.1:8000/chirps?page=1”,“ label”:“ 1”,“ active”:trim},true},{ url“:null,“ label”:“接下来 »“,“ active”:false}],“ next_page_url”:null,“ path”:“ http://127.0.0.1:8000/chirps”,“ per_page”:15,“ prev_page_url” :1,“总计”:1}]
因此,数据库的查询正在工作并且正在获取页面,但是错误是其他的,我已经尝试在功能版本中找到任何分页的特定示例,但没有任何运气。
如果有人可以帮助我理解如何修复此property类型,或者不得不了解是否无法使用功能版本
使用chirp :: with('用户') - > paginate(10)直接在刀片模板中,而无需将其存储在状态下。
将您的伏特组件调整为:
<?php
使用app \ models \ chirp;
使用livewire \ with pagation;
修改刀片视图
<div>
<div class="mt-6 bg-white shadow-sm rounded-lg divide-y">
@foreach ($chirps as $chirp)
<div class="p-6 flex space-x-2" wire:key="{{ $chirp->id }}">
...
</div>
@endforeach
</div>
{{ $chirps->links() }}