56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Post extends Model
|
|
{
|
|
protected $fillable = ['user_id', 'body', 'video_id', 'image'];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function video()
|
|
{
|
|
return $this->belongsTo(Video::class)->withDefault();
|
|
}
|
|
|
|
public function reactions()
|
|
{
|
|
return $this->hasMany(PostReaction::class);
|
|
}
|
|
|
|
public function postImages()
|
|
{
|
|
return $this->hasMany(PostImage::class)->orderBy('sort_order');
|
|
}
|
|
|
|
public function postVideos()
|
|
{
|
|
return $this->hasMany(PostVideo::class)->with('video')->orderBy('sort_order');
|
|
}
|
|
|
|
public function isLikedBy(?User $user): bool
|
|
{
|
|
if (! $user) return false;
|
|
return $this->reactions()->where('user_id', $user->id)->exists();
|
|
}
|
|
|
|
public function getReactionCountAttribute(): int
|
|
{
|
|
return $this->reactions()->count();
|
|
}
|
|
|
|
public function getImageUrlAttribute(): ?string
|
|
{
|
|
if (! $this->image) return null;
|
|
if (str_starts_with($this->image, 'users/')) {
|
|
return route('media.post-image', $this->image);
|
|
}
|
|
return asset('storage/post_images/' . $this->image);
|
|
}
|
|
}
|