-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArticle.php
More file actions
executable file
·77 lines (64 loc) · 1.75 KB
/
Article.php
File metadata and controls
executable file
·77 lines (64 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace App\Models;
use App\Http\Resources\Article\ArticleList;
use App\TechDiary\Reaction\Contracts\ReactableInterface;
use App\TechDiary\Reaction\Traits\ReactionableModel;
use App\Traits\CanBeScoped;
use App\Traits\NestableComments;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
class Article extends Model implements ReactableInterface
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
use HasFactory, CanBeScoped, Searchable, ReactionableModel, NestableComments;
protected $guarded = ['isApproved'];
protected $casts = [
'body' => 'array',
'id' => 'string'
];
protected $primaryKey = 'id';
protected $keyType = 'string';
public function tags()
{
return $this->belongsToMany(Tag::class);
}
public function getRouteKeyName()
{
return 'slug';
}
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public static function boot()
{
parent::boot();
static::creating(function ($article) {
if (!$article->slug) {
$article->slug = Str::slug($article->title) . '-' . Str::random(6);
}
});
}
/**
* Define article payload for algolia search
* @return array
*/
public function toSearchableArray()
{
return ArticleList::make($this->load('tags'))->jsonSerialize();
}
/**
* Defined what object should be searchable
* @return mixed
*/
public function shouldBeSearchable()
{
return $this->isPublished;
}
}