1
|
|
|
<?php namespace Modules\Blog\Entities; |
2
|
|
|
|
3
|
|
|
use Dimsav\Translatable\Translatable; |
4
|
|
|
use Illuminate\Database\Eloquent\Builder; |
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Laracasts\Presenter\PresentableTrait; |
7
|
|
|
use Modules\Blog\Presenters\PostPresenter; |
8
|
|
|
use Modules\Media\Support\Traits\MediaRelation; |
9
|
|
|
|
10
|
|
|
class Post extends Model |
11
|
|
|
{ |
12
|
|
|
use Translatable, MediaRelation, PresentableTrait; |
13
|
|
|
|
14
|
|
|
public $translatedAttributes = ['title', 'slug', 'content']; |
15
|
|
|
protected $fillable = ['category_id', 'status', 'title', 'slug', 'content']; |
16
|
|
|
protected $table = 'blog__posts'; |
17
|
|
|
protected $presenter = PostPresenter::class; |
18
|
|
|
protected $casts = [ |
19
|
|
|
'status' => 'int', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
public function category() |
23
|
|
|
{ |
24
|
|
|
return $this->belongsTo(Category::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function tags() |
28
|
|
|
{ |
29
|
|
|
return $this->belongsToMany(Tag::class, 'blog__post_tag')->withTimestamps(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Check if the post is in draft |
34
|
|
|
* @param Builder $query |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function scopeDraft(Builder $query) |
38
|
|
|
{ |
39
|
|
|
return (bool) $query->whereStatus(0); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check if the post is pending review |
44
|
|
|
* @param Builder $query |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function scopePending(Builder $query) |
48
|
|
|
{ |
49
|
|
|
return (bool) $query->whereStatus(1); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check if the post is published |
54
|
|
|
* @param Builder $query |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function scopePublished(Builder $query) |
58
|
|
|
{ |
59
|
|
|
return (bool) $query->whereStatus(2); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if the post is unpublish |
64
|
|
|
* @param Builder $query |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function scopeUnpublished(Builder $query) |
68
|
|
|
{ |
69
|
|
|
return (bool) $query->whereStatus(3); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $method |
74
|
|
|
* @param $parameters |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function __call($method, $parameters) |
78
|
|
|
{ |
79
|
|
|
#i: Convert array to dot notation |
80
|
|
|
$config = implode('.', ['asgard.blog.config.post.relations', $method]); |
81
|
|
|
|
82
|
|
|
#i: Relation method resolver |
83
|
|
|
if (config()->has($config)) { |
84
|
|
|
$function = config()->get($config); |
85
|
|
|
|
86
|
|
|
return $function($this); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
#i: No relation found, return the call to parent (Eloquent) to handle it. |
90
|
|
|
return parent::__call($method, $parameters); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|