source: app/Models/Post.php@ d25ba66

Last change on this file since d25ba66 was d25ba66, checked in by Özkan İliyaz <iliyaz_96@…>, 4 years ago

ADD post confirmation with multiple phases, notification after succesfully password creation

  • Property mode set to 100644
File size: 1.5 KB
Line 
1<?php
2
3namespace App\Models;
4
5use Carbon\Carbon;
6use Illuminate\Support\Str;
7use Illuminate\Database\Eloquent\Model;
8
9class 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 }
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 }
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}
Note: See TracBrowser for help on using the repository browser.