File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Posts/PostService.php
<?php
namespace App\Komma\Posts;
use App\Komma\Base\Service;
use App\Komma\Posts\Models\Post;
class PostService extends Service
{
/**
* Base query for get post from DB
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
private function basePostQuery()
{
return Post::where('active', 1)
->where('date', '<=', today()->endOfDay())
->with('translation', 'images')
->whereHas('translation', function ($q) {
$q->whereNotNull('name')
->where('name', '!=', '');
})
->orderBy('date', 'desc')
->orderBy('created_at', 'desc');
}
/**
* Get all posts
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getPosts()
{
return $this->basePostQuery()->get();
}
/**
* Get $amount of latest posts
*
* @param int $amount
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getLatestPost(int $amount = 3)
{
return $this->basePostQuery()
->take($amount)
->get();
}
/**
* Get posts paginated per $amount on a page
*
* @param int $amountPerPage
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function getPostsPaginated(int $amountPerPage = 6)
{
return $this->basePostQuery()->paginate($amountPerPage);
}
/**
* Get the next posts after give $post
*
* @param Post $post
* @param int $amount
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getNextPosts(Post $post, int $amount = 3)
{
return $this->basePostQuery()
->where('id', '!=', $post->id)
->where('date', '<=', $post->date->startOfDay())
->take($amount)
->get();
}
}