Laravel raw custom query with pagination

Custom Query with pagination:

Controller:      

public function blogs_list()
{
$title=”List of blog post”;
$data = $this->paginateArray(
DB::select(“SELECT blogs.id,
blogs.title,
blogs.description,
blogs.created_at,
categories.id,
categories.category_name
FROM (categories categories
INNER JOIN post_category_map post_category_map
ON (categories.id = post_category_map.category_id))
RIGHT OUTER JOIN blogs blogs
ON (blogs.id = post_category_map.post_id)
WHERE (categories.id = 1)”));
return view(‘crud.blog_list’)->with(‘title’,$title)->with(‘blogposts’,$data);
}

public function paginateArray($data, $perPage = 5)
{
$page = Paginator::resolveCurrentPage();
$total = count($data);
$results = array_slice($data, ($page – 1) * $perPage, $perPage);
return new LengthAwarePaginator($results, $total, $perPage, $page, [
‘path’ => Paginator::resolveCurrentPath(),
]);
}

View:

<div class=”col-lg-12″>

@foreach($blogposts as $posts)
<div class=”panel panel-default” id=”content-formatting”>
<div class=”panel-heading”>{{ $posts->title }} ID: {{ $posts->id }}</div>
<div class=”panel-body”>
{{ $posts->description }}
<p class=”text-info”>Post Date: {{ $posts->created_at }}</p>
Category:

@foreach($posts->categorys as $category)
<span class=”label label-default”>{{ $category->category_name }} </span>&nbsp;
@endforeach
</div>
</div>
@endforeach

{{ $blogposts->links() }}

</div>

(29)

About Zahidul Hossein Ripon

Zahidul Hossein Ripon a Bangladesh national, born in 1983 did Bachelor of Science in Computer Science and Engineering. At present, Mr. Ripon is working as a Managing Director in Coraltech Solutions. Before working with Coraltech Solutions, he worked in different reputed ICT industries in Bangladesh from year 2007. Mr. Ripon is one of the Associate Member of Bangladesh Computer Society. Mr. Ripon attended couple of Conference and Training, Seminars and events of ICT in Bangladesh and abroad. He is one of the co-author of few number of research publications in international scientific journals and proceedings.
No comments yet.

Leave a Reply


*