|
1
|
|
|
<?php namespace Modules\Blog\Repositories\Eloquent; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
4
|
|
|
use Modules\Blog\Entities\Post; |
|
5
|
|
|
use Modules\Blog\Entities\Status; |
|
6
|
|
|
use Modules\Blog\Repositories\Collection; |
|
7
|
|
|
use Modules\Blog\Repositories\PostRepository; |
|
8
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
|
9
|
|
|
|
|
10
|
|
|
class EloquentPostRepository extends EloquentBaseRepository implements PostRepository |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param int $id |
|
14
|
|
|
* @return object |
|
15
|
|
|
*/ |
|
16
|
|
|
public function find($id) |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->model->with('translations', 'tags')->find($id); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
23
|
|
|
*/ |
|
24
|
|
|
public function all() |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->model->with('translations', 'tags')->orderBy('created_at', 'DESC')->get(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Update a resource |
|
31
|
|
|
* @param $post |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
public function update($post, $data) |
|
36
|
|
|
{ |
|
37
|
|
|
$post->update($data); |
|
38
|
|
|
|
|
39
|
|
|
if (isset($data['tags'])) { |
|
40
|
|
|
$post->tags()->sync($data['tags']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $post; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create a blog post |
|
48
|
|
|
* @param array $data |
|
49
|
|
|
* @return Post |
|
50
|
|
|
*/ |
|
51
|
|
|
public function create($data) |
|
52
|
|
|
{ |
|
53
|
|
|
$post = $this->model->create($data); |
|
54
|
|
|
|
|
55
|
|
|
if (isset($data['tags'])) { |
|
56
|
|
|
$post->tags()->sync($data['tags']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $post; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return all resources in the given language |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $lang |
|
66
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
67
|
|
|
*/ |
|
68
|
|
|
public function allTranslatedIn($lang) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($lang) { |
|
71
|
|
|
$q->where('locale', "$lang"); |
|
72
|
|
|
$q->where('title', '!=', ''); |
|
73
|
|
|
})->with('translations')->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'DESC')->get(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return the latest x blog posts |
|
78
|
|
|
* @param int $amount |
|
79
|
|
|
* @return Collection |
|
80
|
|
|
*/ |
|
81
|
|
|
public function latest($amount = 5) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->model->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->take($amount)->get(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the previous post of the given post |
|
88
|
|
|
* @param object $post |
|
89
|
|
|
* @return object |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getPreviousOf($post) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->model->where('created_at', '<', $post->created_at) |
|
94
|
|
|
->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->first(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the next post of the given post |
|
99
|
|
|
* @param object $post |
|
100
|
|
|
* @return object |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getNextOf($post) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->model->where('created_at', '>', $post->created_at) |
|
105
|
|
|
->whereStatus(Status::PUBLISHED)->first(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Find a resource by the given slug |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $slug |
|
112
|
|
|
* @return object |
|
113
|
|
|
*/ |
|
114
|
|
|
public function findBySlug($slug) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($slug) { |
|
117
|
|
|
$q->where('slug', "$slug"); |
|
118
|
|
|
})->with('translations')->whereStatus(Status::PUBLISHED)->firstOrFail(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|