123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use App\Events\CommentAdded;
- class Comment extends Model
- {
- use SoftDeletes;
- const STATUS_NEW = 0; // Ждут автоматической проверки
- const STATUS_PENDING = 1; // Ждут проверки модератором
- const STATUS_PUBLISHED = 2; // Опубликованы
- const STATUS_REJECTED = 3; // Не прошли проверку
- // Наличие fillable требуется только при использовании mass assignment, т.е. создание записи из ассоциативного массива
- protected $fillable = [
- "name",
- "email",
- "content",
- "status"
- ];
- protected $dispatchesEvents = [
- "created" => CommentAdded::class
- ];
- function commentable() {
- return $this->morphTo();
- }
- function scopePending($query) {
- $query->where("status", static::STATUS_PENDING);
- }
- function scopePublished($query) {
- $query->where("status", static::STATUS_PUBLISHED);
- }
- function scopeRecent($query) {
- $query->orderBy("created_at", "desc");
- }
- function makePending() {
- $this->status = static::STATUS_PENDING;
- $this->save();
- }
- function allow() {
- $this->status = static::STATUS_PUBLISHED;
- $this->save();
- }
- function reject() {
- $this->status = static::STATUS_REJECTED;
- $this->save();
- }
- }
|