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