[0924b6c] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Models;
|
---|
| 4 |
|
---|
| 5 | use Carbon\Carbon;
|
---|
| 6 | use Illuminate\Support\Str;
|
---|
| 7 | use Illuminate\Database\Eloquent\Model;
|
---|
| 8 |
|
---|
| 9 | class Post extends Model
|
---|
| 10 | {
|
---|
| 11 | protected $table = "posts";
|
---|
| 12 |
|
---|
| 13 | protected $fillable = ["title", "image_link", "content", "user_id", "category_id"];
|
---|
| 14 |
|
---|
| 15 | public function user() {
|
---|
| 16 | return $this->belongsTo(User::class);
|
---|
| 17 | }
|
---|
[d25ba66] | 18 |
|
---|
| 19 | public function review() {
|
---|
| 20 | return $this->hasOne(Review::class);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public function needReview()
|
---|
| 24 | {
|
---|
| 25 | return PostSecurity::whereRoleId($this->user->role->id)->first();
|
---|
| 26 | }
|
---|
[0924b6c] | 27 |
|
---|
| 28 | public function category() {
|
---|
| 29 | return $this->belongsTo(Category::class);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public function tag()
|
---|
| 33 | {
|
---|
| 34 | return $this->belongsToMany(Tag::class, "posts_tags");
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public function comment() {
|
---|
| 38 | return $this->hasMany(Comment::class);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public function like()
|
---|
| 42 | {
|
---|
| 43 | return $this->hasMany(Like::class);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public function getConfirmedBy() {
|
---|
| 47 | return User::find($this->confirmed_by)->getFullName();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public function ago() {
|
---|
| 51 | return Carbon::parse($this->created_at)->diffForHumans();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public function createSlug($isEdit = false) {
|
---|
| 55 |
|
---|
| 56 | $newSlug = Str::slug($this->title);
|
---|
| 57 | $slugs = Post::pluck("slug");
|
---|
| 58 |
|
---|
| 59 | if($isEdit) {
|
---|
| 60 | return $newSlug;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if($slugs->contains($newSlug)) {
|
---|
| 64 | $id = $this->latest("id")->first()->id + 1;
|
---|
| 65 | $newSlug = $newSlug . "-" . $id;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return $newSlug;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public function isLiked($ipAddress) {
|
---|
| 72 |
|
---|
| 73 | $likes = $this->like->all();
|
---|
| 74 |
|
---|
| 75 | foreach($likes as $like) {
|
---|
| 76 | if($this->id == $like->post_id && $ipAddress == $like->ip_address) {
|
---|
| 77 | return true;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | return false;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|