61 lines
1.1 KiB
PHP
61 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class News extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'title',
|
|
'slug',
|
|
'content',
|
|
'category',
|
|
'image',
|
|
'status',
|
|
'views',
|
|
];
|
|
|
|
protected $casts = [
|
|
'views' => 'integer',
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($news) {
|
|
if (empty($news->slug)) {
|
|
$news->slug = Str::slug($news->title);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function scopePublished($query)
|
|
{
|
|
return $query->where('status', 'published');
|
|
}
|
|
|
|
public function scopeByCategory($query, $category)
|
|
{
|
|
return $query->where('category', $category);
|
|
}
|
|
|
|
public function incrementViews()
|
|
{
|
|
$this->increment('views');
|
|
}
|
|
|
|
public function getImageUrlAttribute()
|
|
{
|
|
if ($this->image) {
|
|
return asset('storage/' . $this->image);
|
|
}
|
|
return null;
|
|
}
|
|
}
|