1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Models\NewsItem; |
6
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class NewsItemRepository |
10
|
|
|
{ |
11
|
|
|
public static function getAll(): Collection |
12
|
|
|
{ |
13
|
|
|
return NewsItem::orderBy('publish_date', 'desc')->get(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function getLatest(int $amount): Collection |
17
|
|
|
{ |
18
|
|
|
return NewsItem::orderBy('publish_date', 'desc') |
19
|
|
|
->take($amount) |
20
|
|
|
->get(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function findById(int $id): NewsItem |
24
|
|
|
{ |
25
|
|
|
return NewsItem::findOrFail($id); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function findBySlug(string $slug): NewsItem |
29
|
|
|
{ |
30
|
|
|
return NewsItem::where('slug->'.content_locale(), $slug)->firstOrFail(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \App\Models\NewsItem|null |
35
|
|
|
*/ |
36
|
|
|
public static function findNext(NewsItem $newsItem) |
37
|
|
|
{ |
38
|
|
|
return NewsItem::online() |
39
|
|
|
->where('publish_date', '>', $newsItem->publish_date) |
40
|
|
|
->orderBy('publish_date', 'desc') |
41
|
|
|
->first(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return \App\Models\NewsItem|null |
46
|
|
|
*/ |
47
|
|
|
public static function findPrevious(NewsItem $newsItem) |
48
|
|
|
{ |
49
|
|
|
return NewsItem::online() |
50
|
|
|
->where('publish_date', '<', $newsItem->publish_date) |
51
|
|
|
->orderBy('publish_date', 'desc') |
52
|
|
|
->first(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function paginate(int $perPage): Paginator |
56
|
|
|
{ |
57
|
|
|
return NewsItem::online()->paginate($perPage); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|