File: D:/HostingSpaces/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Posts/PostService.php
<?php
namespace App\KommaApp\Posts;
use App\KommaApp\Posts\Models\Post;
use Carbon\Carbon;
class PostService
{
private $today;
public function __construct()
{
$this->today = Carbon::now()->addHour();
$this->today = $this->today->format('Y-m-d H:i:s');
}
public function getAllPosts($pagination = false, $itemsPerPage = 8, $skipFirst = false)
{
$postQuery = \App::getSite()
->posts()
->with('translation', 'images', 'author', 'author.images')
->where('active', 1)
->where('date', '<=', $this->today)
->orderBy('date','desc')
->orderBy('created_at', 'desc');
$firstPostQuery = clone $postQuery;
$firstPost = $firstPostQuery->first();
if($skipFirst) $posts = $postQuery->where('posts.id', '!=', $firstPost->id);
else $posts = $postQuery;
if($pagination) $posts = $posts->paginate($itemsPerPage);
else $posts = $posts->get();
if($skipFirst)
{
return (object)[
'firstPost' => $firstPost,
'posts' => $posts
];
}
return $posts;
}
}