You've already forked Atomcms-edit
100 lines
3.4 KiB
PHP
Executable File
100 lines
3.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Articles;
|
|
|
|
use App\Models\Concerns\BelongsToUser;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Database\Factories\Articles\WebsiteArticleFactory;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Spatie\Sluggable\HasSlug;
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $title
|
|
* @property string $slug
|
|
* @property string $short_story
|
|
* @property string $full_story
|
|
* @property string $image
|
|
* @property int $user_id
|
|
* @property Carbon|null $deleted_at
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property-read User|null $user
|
|
* @property-read Collection<int, WebsiteArticleReaction> $reactions
|
|
* @property-read Collection<int, WebsiteArticleComment> $comments
|
|
* @property-read Collection<int, Tag> $tags
|
|
*
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle where($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle whereNot($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle whereHas($relation, \Closure $callback = null, $operator = '>=', $count = 1)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle latest($column = 'created_at')
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle get($columns = ['*'])
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle take(int $value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle with($relations)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle has($relation, \Closure $callback = null, $operator = '>=', $count = 1)
|
|
*/
|
|
class WebsiteArticle extends Model
|
|
{
|
|
use BelongsToUser;
|
|
use HasSlug, \Illuminate\Database\Eloquent\Factories\HasFactory, SoftDeletes;
|
|
|
|
/** @var array<int, string> */
|
|
#[\Override]
|
|
protected $with = ['user'];
|
|
|
|
protected static function newFactory()
|
|
{
|
|
return WebsiteArticleFactory::new();
|
|
}
|
|
|
|
#[\Override]
|
|
protected $guarded = ['id', 'created_at', 'updated_at', 'user_id', 'slug', 'visible', 'fixed'];
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
{
|
|
return SlugOptions::create()
|
|
->generateSlugsFrom('title')
|
|
->saveSlugsTo('slug')
|
|
->usingSeparator('-')->allowDuplicateSlugs();
|
|
}
|
|
|
|
public function reactions(): HasMany
|
|
{
|
|
return $this->hasMany(WebsiteArticleReaction::class, 'article_id')
|
|
->whereActive(true);
|
|
}
|
|
|
|
public function comments(): HasMany
|
|
{
|
|
return $this->hasMany(WebsiteArticleComment::class, 'article_id');
|
|
}
|
|
|
|
public function userHasReachedArticleCommentLimit(): bool
|
|
{
|
|
return $this->comments()->where('user_id', '=', Auth::id())->count() >= (int) setting('max_comment_per_article');
|
|
}
|
|
|
|
#[\Override]
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::saving(function ($model) {
|
|
if (empty($model->image)) {
|
|
$model->image = '';
|
|
}
|
|
});
|
|
}
|
|
|
|
public function tags()
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
}
|