You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
PHP
67 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Noticia extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'noticias';
|
|
|
|
protected $fillable = [
|
|
'titulo',
|
|
'slug',
|
|
'descripcion_corta',
|
|
'contenido',
|
|
'categoria',
|
|
'tag_color',
|
|
'imagen_path',
|
|
'imagen_url', // ✅ agrega esto si también lo guardas en BD
|
|
'link_url',
|
|
'link_texto',
|
|
'fecha_publicacion',
|
|
'publicado',
|
|
'destacado',
|
|
'orden',
|
|
];
|
|
|
|
protected $casts = [
|
|
'fecha_publicacion' => 'datetime',
|
|
'publicado' => 'boolean',
|
|
'destacado' => 'boolean',
|
|
'orden' => 'integer',
|
|
];
|
|
|
|
// ✅ se incluirá en el JSON
|
|
protected $appends = ['imagen_url'];
|
|
|
|
public function getImagenUrlAttribute(): ?string
|
|
{
|
|
// 1) Si en BD hay una URL externa, úsala
|
|
if (!empty($this->attributes['imagen_url'])) {
|
|
return $this->attributes['imagen_url'];
|
|
}
|
|
|
|
// 2) Si hay imagen en storage, genera URL absoluta
|
|
if (!empty($this->imagen_path)) {
|
|
return url(Storage::disk('public')->url($this->imagen_path));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (Noticia $noticia) {
|
|
if (!$noticia->slug) {
|
|
$noticia->slug = Str::slug($noticia->titulo);
|
|
}
|
|
});
|
|
}
|
|
}
|