| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Model; |
| 6 |
use FluentCommunity\App\Models\XProfile; |
| 7 |
|
| 8 |
class Reaction extends Model |
| 9 |
{ |
| 10 |
protected $table = 'fcom_post_reactions'; |
| 11 |
|
| 12 |
protected $guarded = ['id']; |
| 13 |
|
| 14 |
protected $fillable = [ |
| 15 |
'user_id', |
| 16 |
'object_id', |
| 17 |
'object_type', |
| 18 |
'type', |
| 19 |
'ip_address', |
| 20 |
'parent_id', |
| 21 |
'created_at', |
| 22 |
'updated_at' |
| 23 |
]; |
| 24 |
|
| 25 |
protected $hidden = ['ip_address']; |
| 26 |
|
| 27 |
protected $searchable = [ |
| 28 |
'message' |
| 29 |
]; |
| 30 |
|
| 31 |
public static function boot() |
| 32 |
{ |
| 33 |
parent::boot(); |
| 34 |
|
| 35 |
static::creating(function ($model) { |
| 36 |
|
| 37 |
if (empty($model->user_id)) { |
| 38 |
$model->user_id = get_current_user_id(); |
| 39 |
} |
| 40 |
|
| 41 |
if (empty($model->object_type)) { |
| 42 |
$model->object_type = 'feed'; |
| 43 |
} |
| 44 |
|
| 45 |
if (empty($model->type)) { |
| 46 |
$model->type = 'like'; |
| 47 |
} |
| 48 |
|
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
public function scopeTypeBy($query, $type = 'like') |
| 53 |
{ |
| 54 |
if ($type) { |
| 55 |
$query->where('type', $type); |
| 56 |
} |
| 57 |
|
| 58 |
return $query; |
| 59 |
} |
| 60 |
|
| 61 |
public function scopeObjectType($query, $type = 'feed') |
| 62 |
{ |
| 63 |
if ($type) { |
| 64 |
$query->where('object_type', $type); |
| 65 |
} |
| 66 |
|
| 67 |
return $query; |
| 68 |
} |
| 69 |
|
| 70 |
public function user() |
| 71 |
{ |
| 72 |
return $this->belongsTo(User::class, 'user_id', 'ID'); |
| 73 |
} |
| 74 |
|
| 75 |
public function xprofile() |
| 76 |
{ |
| 77 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 78 |
} |
| 79 |
|
| 80 |
public function feed() |
| 81 |
{ |
| 82 |
return $this->belongsTo(Feed::class, 'object_id', 'id') |
| 83 |
->where('object_type', 'feed'); |
| 84 |
} |
| 85 |
|
| 86 |
public function comment() |
| 87 |
{ |
| 88 |
return $this->belongsTo(Comment::class, 'object_id', 'id') |
| 89 |
->where('object_type', 'comment'); |
| 90 |
} |
| 91 |
} |
| 92 |
|