/
Post.php
54 lines (40 loc) · 1.16 KB
/
Post.php
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
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;
class Post extends Model
{
protected $dates = ['published_at'];
protected $fillable = ['category_id'];
public function getImageUrlAttribute($value){
$imageUrl = "";
if(!is_null($this->image)){
$imageUrl = $this->image;
}else{
$imageUrl = "";
}
return $imageUrl;
}
public function author(){
return $this->belongsTo(User::class);
}
public function category(){
return $this->belongsTo(Category::class);
}
public function getDateAttribute($value){
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}
public function getBodyHtmlAttribute($value){
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL ;
}
public function getExcerptHtmlAttribute($value){
return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL ;
}
public function scopeLatestFirst($query){
return $query->orderBy('created_at', 'desc');
}
public function scopePublished($query){
return $query->where("published_at", "<=", Carbon::now());
}
}