|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oscer\Cms\Core\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
6
|
|
|
use Illuminate\Support\Carbon; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
use League\CommonMark\Block\Element\FencedCode; |
|
10
|
|
|
use League\CommonMark\Block\Element\IndentedCode; |
|
11
|
|
|
use League\CommonMark\CommonMarkConverter; |
|
12
|
|
|
use League\CommonMark\Environment; |
|
13
|
|
|
use Spatie\CommonMarkHighlighter\FencedCodeRenderer; |
|
14
|
|
|
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer; |
|
15
|
|
|
use Spatie\Sluggable\HasSlug; |
|
16
|
|
|
use Spatie\Sluggable\SlugOptions; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @property int id |
|
20
|
|
|
* @property string name |
|
21
|
|
|
* @property string slug |
|
22
|
|
|
* @property string body |
|
23
|
|
|
* @property \Oscer\Cms\Core\Models\User author |
|
24
|
|
|
* @property int author_id |
|
25
|
|
|
* @property Collection tags |
|
26
|
|
|
* @property Carbon|null published_at |
|
27
|
|
|
* @property Carbon updated_at |
|
28
|
|
|
* @property Carbon created_at |
|
29
|
|
|
*/ |
|
30
|
|
|
class Post extends BaseModel |
|
31
|
|
|
{ |
|
32
|
|
|
use HasSlug; |
|
33
|
|
|
|
|
34
|
|
|
protected $table = 'cms_posts'; |
|
35
|
|
|
|
|
36
|
|
|
protected $with = ['tags', 'author']; |
|
37
|
|
|
|
|
38
|
|
|
protected static function booted() |
|
39
|
|
|
{ |
|
40
|
|
|
static::creating(function (self $post) { |
|
41
|
|
|
if (! $post->author_id) { |
|
42
|
|
|
$post->author_id = auth()->user()->id; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
$post->type = $post->getType(); |
|
45
|
|
|
}); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getType() |
|
49
|
|
|
{ |
|
50
|
|
|
return 'post'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
55
|
|
|
*/ |
|
56
|
|
|
public function author() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->belongsTo(\Oscer\Cms\Core\Models\User::class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getSlugOptions(): SlugOptions |
|
62
|
|
|
{ |
|
63
|
|
|
return SlugOptions::create() |
|
64
|
|
|
->generateSlugsFrom('name') |
|
65
|
|
|
->saveSlugsTo('slug') |
|
66
|
|
|
->doNotGenerateSlugsOnUpdate(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function tags(): BelongsToMany |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->belongsToMany(Tag::class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* This method allows to set the tags on a post via the property. |
|
76
|
|
|
* If it is a new model they will be synced in a event callback. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setTagsAttribute(array $value) |
|
79
|
|
|
{ |
|
80
|
|
|
/* |
|
81
|
|
|
* First we retrieve or create all used tags and pluck the id's |
|
82
|
|
|
*/ |
|
83
|
|
|
$tags = collect($value) |
|
84
|
|
|
->map(function (string $name) { |
|
85
|
|
|
return Tag::query()->firstOrCreate(['name' => $name]); |
|
86
|
|
|
}) |
|
87
|
|
|
->pluck('id'); |
|
88
|
|
|
|
|
89
|
|
|
/* |
|
90
|
|
|
* If the post exists we can simply sync the tags with the post. But if |
|
91
|
|
|
* we do not have the required id for the pivot table, we register a |
|
92
|
|
|
* `created` callback which syncs the tags with the post. |
|
93
|
|
|
*/ |
|
94
|
|
|
if ($this->id !== null) { |
|
95
|
|
|
$this->tags()->sync($tags); |
|
96
|
|
|
} else { |
|
97
|
|
|
self::created(function (self $post) use ($tags) { |
|
98
|
|
|
$post->tags()->sync($tags); |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getRenderedBody() |
|
104
|
|
|
{ |
|
105
|
|
|
$languages = ['php', 'bash', 'yaml', 'ini', 'dockerfile']; |
|
106
|
|
|
$env = Environment::createCommonMarkEnvironment(); |
|
107
|
|
|
$env->addBlockRenderer(FencedCode::class, new FencedCodeRenderer($languages)); |
|
108
|
|
|
$env->addBlockRenderer(IndentedCode::class, new IndentedCodeRenderer($languages)); |
|
109
|
|
|
$converter = new CommonMarkConverter([], $env); |
|
110
|
|
|
|
|
111
|
|
|
return $converter->convertToHtml($this->body); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function joiningTableSegment() |
|
115
|
|
|
{ |
|
116
|
|
|
return 'post'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getFeaturedImageAttribute($value) |
|
120
|
|
|
{ |
|
121
|
|
|
return $value ? Storage::url($value) : null; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
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: