-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.php
46 lines (37 loc) · 977 Bytes
/
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
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Post extends Model
{
protected $dates = ['published_at'];
public function getImageUrlAttribute($value)
{
$imageUrl = "";
if( ! is_null ($this->image))
{
$imagePath = public_path(). "/blog/img/".$this->image;
if(file_exists($imagePath)) $imageUrl = asset("blog/img/".$this->image);
}
return $imageUrl;
}
public function author()
{
# satu post milik dari satu user
return $this->belongsTo(User::class);
}
// membuat mutator
public function getDateAttribute($value)
{
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}
// membuat scope
public function scopeLatestFirst()
{
return $this->orderBy('published_at','desc');
}
public function scopePublished()
{
return $this->where("published_at","<=",Carbon::now());
}
}