first change
This commit is contained in:
90
Modules/Content/app/Models/Content.php
Normal file
90
Modules/Content/app/Models/Content.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Content\Models;
|
||||
|
||||
use App\Traits\CreatedUpdatedBy;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Modules\Admin\Models\ActivityLog;
|
||||
use Modules\Product\Models\Product;
|
||||
|
||||
class Content extends Model
|
||||
{
|
||||
use HasFactory, CreatedUpdatedBy;
|
||||
|
||||
const STATUS = [
|
||||
11 => 'Draft',
|
||||
12 => 'Scheduled',
|
||||
13 => 'Published',
|
||||
14 => 'Archived',
|
||||
];
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'product_id',
|
||||
'category_id',
|
||||
'release_date',
|
||||
'release_time',
|
||||
'creative',
|
||||
'caption',
|
||||
'desc',
|
||||
'order',
|
||||
'createdby',
|
||||
'updatedby',
|
||||
'status',
|
||||
'remarks',
|
||||
];
|
||||
|
||||
protected $appends = ['status_name'];
|
||||
|
||||
protected $casts = [
|
||||
'release_date' => 'date:Y-m-d',
|
||||
'release_time' => 'datetime:H:i:s',
|
||||
];
|
||||
|
||||
protected function statusName(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
switch ($attributes['status']) {
|
||||
case '11':
|
||||
return '<span class="badge bg-dark">' . self::STATUS[$attributes['status']] . '</span>';
|
||||
case '12':
|
||||
return '<span class="badge bg-success">' . self::STATUS[$attributes['status']] . '</span>';
|
||||
case '13':
|
||||
return '<span class="badge bg-primary">' . self::STATUS[$attributes['status']] . '</span>';
|
||||
case '14':
|
||||
return '<span class="badge bg-danger">' . self::STATUS[$attributes['status']] . '</span>';
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
},
|
||||
set: fn($value) => $value,
|
||||
);
|
||||
}
|
||||
|
||||
public function product()
|
||||
{
|
||||
return $this->belongsTo(Product::class, 'product_id');
|
||||
}
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(ContentCategory::class, 'category_id');
|
||||
}
|
||||
|
||||
public function activityLogs(): MorphMany
|
||||
{
|
||||
return $this->morphMany(ActivityLog::class, 'loggable')->latest();
|
||||
}
|
||||
|
||||
public function createLog($data)
|
||||
{
|
||||
return $this->activityLogs()->create($data);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user