livewire pagination
composer require livewire/livewire
Edit the component class app/Http/Livewire/Posts.php:
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Post;
class Posts extends Component
{
use WithPagination;
public function render()
{
$posts = Post::paginate(5);
return view('livewire.posts', ['posts' => $posts]);
}
}
Edit the view file resources/views/livewire/posts.blade.php:
<div>
<h1>Posts</h1>
@foreach ($posts as $post)
<div class="post">
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
</div>
@endforeach
{{ $posts->links() }}
</div>
resources/views/layouts/app.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Livewire Pagination Demo</title>
@livewireStyles
</head>
<body>
<div class="container">
@yield('content')
</div>
@livewireScripts
</body>
</html>
<livewire:posts lazy>
Tags:
laravel