How to create blog using Laravel 5.3 - How to create Pagination (paginate method) using Eloquent and using Query builder in laravel 5.3.
Previews lessons, we have learned about simple CRUD operation in laravel 5.3, strarting from Create new data/post into database, edit the data, delete data from databases, validation and more.
if you still confused, just read our previews lessons :
- Deleting Data Using Eloquent Method in Laravel 5.3
- Update Database Recorded in laravel 5.3
- Create Data, Validation, Redirect Page and Flash Message
Pagination with Eloquent & Query Builder in Laravel 5.3
To create pagination in laravel project, we two method to do that, first Pagination using Query builder and next pagination using Eloquent.Pagination with Query Builder
at the our controller (BlogController.php) edit the Index function like this
BlogController.php
public function index()
{
// we will create index function
// we need to show all data from "blog" table
// $blogs = Blog::all();
// first, pagination using query builder
$blogs = DB::table('blog_post')->paginate(2);
// pagination using Eloquent
// $blogs = Blog::paginate(2);
// show data to our view
return view('blog.index',['blogs' => $blogs]);
}
Pagination with Eloquent ORM
BlogController.php
public function index()
{
// we will create index function
// we need to show all data from "blog" table
// $blogs = Blog::all();
// first, pagination using query builder
// $blogs = DB::table('blog_post')->paginate(2);
// pagination using Eloquent
$blogs = Blog::paginate(2);
// show data to our view
return view('blog.index',['blogs' => $blogs]);
}
Next, at the our index add link to pagination,
index.blade.php
{{ Session::get('message') }}
<h1>My First Blog</h1>
@foreach ($blogs as $blog)
<h2><a href="/blog/{{ $blog->id }}">{{ $blog->title }}</a></h2>
{{ date('F d, Y', strtotime($blog->created_at)) }}
<p>{{ $blog->description }}</p>
<a href="/blog/{{ $blog->id }}/edit">Edit this Post</a><br>
<form class="" action="/blog/{{ $blog->id }}" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" name="name" value="delete">
</form>
<hr>
@endforeach
{!! $blogs->links() !!}
Video Tutorial Pagination with Eloquent & Query Builder in Laravel 5.3
See you Next Lessons ..............
Không có nhận xét nào:
Đăng nhận xét