<?php
namespace App\Models;
use Awcodes\Curator\Models\Media;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'slug',
'image_id',
'description',
'is_active'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'is_active' => 'boolean',
];
public function image(): BelongsTo
{
return $this->belongsTo(Media::class, 'image_id', 'id');
}
public function courses(): HasMany
{
return $this->hasMany(Course::class);
}
public function getImageUrlAttribute()
{
// Check if image relationship is loaded
if (!$this->relationLoaded('image')) {
return config('app.placeholder_image_url');
}
// Access image URL through the relationship
return $this->image->path;
}
<?php
namespace App\Models;
use Awcodes\Curator\Models\Media;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'slug',
'image_id',
'description',
'is_active'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'is_active' => 'boolean',
];
public function image(): BelongsTo
{
return $this->belongsTo(Media::class, 'image_id', 'id');
}
public function courses(): HasMany
{
return $this->hasMany(Course::class);
}
public function getImageUrlAttribute()
{
// Check if image relationship is loaded
if (!$this->relationLoaded('image')) {
return config('app.placeholder_image_url');
}
// Access image URL through the relationship
return $this->image->path;
}