| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 | 
							- <?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_PENDING	= 0;
 
- 	const STATUS_PUBLISHED	= 1;
 
- 	const STATUS_REJECTED	= 2;
 
- 	// Наличие 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 allow() {
 
- 		$this->status = static::STATUS_PUBLISHED;
 
- 		$this->save();
 
- 	}
 
- 	function reject() {
 
- 		$this->status = static::STATUS_REJECTED;
 
- 		$this->save();
 
- 	}
 
- }
 
 
  |