1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use App\Traits\Sluggable; |
6
|
|
|
use Laravel\Scout\Searchable; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class Post extends Model |
10
|
|
|
{ |
11
|
|
|
use Sluggable, Searchable { |
12
|
|
|
searchable as scoutSearchable; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public $dates = ['published_at']; |
16
|
|
|
|
17
|
|
|
public function category() |
18
|
|
|
{ |
19
|
|
|
return $this->belongsTo(Category::class); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function tags() |
23
|
|
|
{ |
24
|
|
|
return $this->belongsToMany(Tag::class, 'posts_tags'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getBodyMarkdownAttribute() |
28
|
|
|
{ |
29
|
|
|
return (new \Parsedown())->text($this->body); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getBodyRawAttribute() |
33
|
|
|
{ |
34
|
|
|
return strip_tags($this->body_markdown); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getUrlAttribute() |
38
|
|
|
{ |
39
|
|
|
return route('posts.show', $this->slug); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isVisible() |
43
|
|
|
{ |
44
|
|
|
return $this->attributes['visible']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function scopePublished($query) |
48
|
|
|
{ |
49
|
|
|
return $query->where('published_at', '<=', date('Y-m-d H:i:s')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function scopeVisible($query) |
53
|
|
|
{ |
54
|
|
|
return $query->where('visible', true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function scopeFeatured($query) |
58
|
|
|
{ |
59
|
|
|
return $query->where('featured', true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function scopeRecentsFirst($query) |
63
|
|
|
{ |
64
|
|
|
return $query->orderBy('published_at', 'DESC'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function scopeWhereHasTag($query, ...$tags) |
68
|
|
|
{ |
69
|
|
|
return $query->whereHas('tags', function ($queryTags) use ($tags) { |
70
|
|
|
return $queryTags->whereIn('slug', $tags); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function scopeSearchLike($query, $search) |
75
|
|
|
{ |
76
|
|
|
return $query->where(function ($post) use ($search) { |
77
|
|
|
return $post->where('title', 'like', "%{$search}%") |
78
|
|
|
->orWhere('published_at', 'like', "%{$search}%") |
79
|
|
|
->orWhere('body', 'like', "%{$search}%") |
80
|
|
|
->orWhereHas('tags', function ($tags) use ($search) { |
81
|
|
|
return $tags->where('name', 'like', "%{$search}%"); |
82
|
|
|
}) |
83
|
|
|
->orWhereHas('category', function ($category) use ($search) { |
84
|
|
|
return $category->where('name', 'like', "%{$search}%"); |
85
|
|
|
}); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function syncTags($rawTags) |
90
|
|
|
{ |
91
|
|
|
$tags = collect(array_filter($rawTags))->map(function ($rawTag) { |
92
|
|
|
$tag = Tag::findOrCreateByName(trim($rawTag)); |
93
|
|
|
|
94
|
|
|
return $tag; |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$this->tags()->sync($tags->pluck('id')->toArray()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function searchable() |
101
|
|
|
{ |
102
|
|
|
if ($this->isVisible() && $this->published_at->isPast()) { |
103
|
|
|
$this->scoutSearchable(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function toSearchableArray() |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
|
|
'id' => $this->id, |
111
|
|
|
'title' => $this->title, |
112
|
|
|
'slug' => $this->slug, |
113
|
|
|
'body' => $this->body, |
114
|
|
|
'published_at' => $this->published_at, |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|