Here is my use case.
- I have to fetch blog data by writing sql query and the data needs to be paginated.
- The data fetched from above query needs to be looped to create image url for each blog.
- After looping each blog record I create a new dimensional array which has all the blog details along with image url
below is the code sample with which I want to fetch all blog data with process image url and I also expect it has pagination details like the total no of pages, current page etc.
public function getBlogs(Request $request)
{
$rawBlogs = DB::table('blogs')
->select(['id', 'title', 'content', 'status', 'blog_image'])
->where([
['status', '=', 'A']
])
->orderBy('id', 'DESC')
->paginate();
$Blogs = [];
foreach($rawBlogs-items() as $rawBlog){
$blog_path = config('app.BLOG_IMAGE_PATH');
$imgPath = $blog_path . $rawBlog->blog_image;
$Url = \Storage::url($imgPath);
$Blogs[] = [
'id' => $rawBlog->id,
'title' => $rawBlog->title,
'content' => $rawBlog->content,
'status' => $rawBlog->status,
'blog_image' => $Url
];
}
return [
'data' => $Blogs,
current_page => ?,
first_page_url => ?,
from => ?,
next_page_url => ?,
first_page_url => ?,
path => ?,
per_page => ?,
prev_page_url => ?,
total => ?
];
}