1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Blog\Repositories\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Modules\Blog\Entities\Post; |
7
|
|
|
use Modules\Blog\Entities\Status; |
8
|
|
|
use Modules\Blog\Events\PostIsCreating; |
9
|
|
|
use Modules\Blog\Events\PostIsUpdating; |
10
|
|
|
use Modules\Blog\Events\PostWasCreated; |
11
|
|
|
use Modules\Blog\Events\PostWasDeleted; |
12
|
|
|
use Modules\Blog\Events\PostWasUpdated; |
13
|
|
|
use Modules\Blog\Repositories\Collection; |
14
|
|
|
use Modules\Blog\Repositories\PostRepository; |
15
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
16
|
|
|
|
17
|
|
|
class EloquentPostRepository extends EloquentBaseRepository implements PostRepository |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param int $id |
21
|
|
|
* @return object |
22
|
|
|
*/ |
23
|
|
|
public function find($id) |
24
|
|
|
{ |
25
|
|
|
return $this->model->with('translations', 'tags')->find($id); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
30
|
|
|
*/ |
31
|
|
|
public function all() |
32
|
|
|
{ |
33
|
|
|
return $this->model->with('translations', 'tags')->orderBy('created_at', 'DESC')->get(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Update a resource |
38
|
|
|
* @param $post |
39
|
|
|
* @param array $data |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
3 |
|
public function update($post, $data) |
43
|
|
|
{ |
44
|
3 |
|
event($event = new PostIsUpdating($post, $data)); |
45
|
3 |
|
$post->update($event->getAttributes()); |
46
|
|
|
|
47
|
3 |
|
$post->tags()->sync(array_get($data, 'tags', [])); |
48
|
|
|
|
49
|
3 |
|
event(new PostWasUpdated($post, $data)); |
50
|
|
|
|
51
|
3 |
|
return $post; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create a blog post |
56
|
|
|
* @param array $data |
57
|
|
|
* @return Post |
58
|
|
|
*/ |
59
|
9 |
|
public function create($data) |
60
|
|
|
{ |
61
|
9 |
|
event($event = new PostIsCreating($data)); |
62
|
9 |
|
$post = $this->model->create($event->getAttributes()); |
|
|
|
|
63
|
|
|
|
64
|
9 |
|
$post->tags()->sync(array_get($data, 'tags', [])); |
65
|
|
|
|
66
|
9 |
|
event(new PostWasCreated($post, $data)); |
67
|
|
|
|
68
|
9 |
|
return $post; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function destroy($model) |
72
|
|
|
{ |
73
|
|
|
event(new PostWasDeleted($model->id, get_class($model))); |
74
|
|
|
|
75
|
|
|
return $model->delete(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return all resources in the given language |
80
|
|
|
* |
81
|
|
|
* @param string $lang |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
83
|
|
|
*/ |
84
|
1 |
|
public function allTranslatedIn($lang) |
85
|
|
|
{ |
86
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($lang) { |
87
|
1 |
|
$q->where('locale', "$lang"); |
88
|
1 |
|
$q->where('title', '!=', ''); |
89
|
1 |
|
})->with('translations')->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'DESC')->get(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the latest x blog posts |
94
|
|
|
* @param int $amount |
95
|
|
|
* @return Collection |
96
|
|
|
*/ |
97
|
|
|
public function latest($amount = 5) |
98
|
|
|
{ |
99
|
|
|
return $this->model->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->take($amount)->get(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the previous post of the given post |
104
|
|
|
* @param object $post |
105
|
|
|
* @return object |
106
|
|
|
*/ |
107
|
|
|
public function getPreviousOf($post) |
108
|
|
|
{ |
109
|
|
|
return $this->model->where('created_at', '<', $post->created_at) |
110
|
|
|
->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->first(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the next post of the given post |
115
|
|
|
* @param object $post |
116
|
|
|
* @return object |
117
|
|
|
*/ |
118
|
|
|
public function getNextOf($post) |
119
|
|
|
{ |
120
|
|
|
return $this->model->where('created_at', '>', $post->created_at) |
121
|
|
|
->whereStatus(Status::PUBLISHED)->first(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Find a resource by the given slug |
126
|
|
|
* |
127
|
|
|
* @param string $slug |
128
|
|
|
* @return object |
129
|
|
|
*/ |
130
|
|
|
public function findBySlug($slug) |
131
|
|
|
{ |
132
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($slug) { |
133
|
|
|
$q->where('slug', "$slug"); |
134
|
|
|
})->with('translations')->whereStatus(Status::PUBLISHED)->firstOrFail(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: