You've already forked Atomcms-edit
60 lines
1.5 KiB
PHP
Executable File
60 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Articles;
|
|
|
|
use App\Models\Concerns\BelongsToUser;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticleReaction where($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticleReaction first($columns = ['*'])
|
|
*/
|
|
class WebsiteArticleReaction extends Model
|
|
{
|
|
use BelongsToUser;
|
|
use HasFactory;
|
|
|
|
#[\Override]
|
|
protected $guarded = ['id', 'user_id', 'article_id'];
|
|
|
|
#[\Override]
|
|
public $timestamps = false;
|
|
|
|
#[\Override]
|
|
protected $hidden = [
|
|
'user_id',
|
|
'article_id',
|
|
];
|
|
|
|
/**
|
|
* @return static|null
|
|
*/
|
|
public static function getReaction(int $articleId, int $userId, string $reaction): ?self
|
|
{
|
|
/** @var static|null $reaction */
|
|
$reaction = self::where('user_id', $userId)
|
|
->where('article_id', $articleId)
|
|
->where('reaction', $reaction)
|
|
->first();
|
|
|
|
return $reaction;
|
|
}
|
|
|
|
#[\Override]
|
|
public static function boot(): void
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
$model->user_id = auth()->id();
|
|
});
|
|
}
|
|
|
|
public function article(): BelongsTo
|
|
{
|
|
return $this->belongsTo(WebsiteArticle::class, 'article_id');
|
|
}
|
|
}
|