77 lines
1.7 KiB
PHP
Executable File
77 lines
1.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Producto extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'productos';
|
|
|
|
protected $fillable = [
|
|
'nombre',
|
|
'descripcion',
|
|
'precio',
|
|
'imagen',
|
|
'categoria',
|
|
'destacado',
|
|
'activo',
|
|
'orden',
|
|
];
|
|
|
|
protected $casts = [
|
|
'precio' => 'decimal:2',
|
|
'destacado' => 'boolean',
|
|
'activo' => 'boolean',
|
|
'orden' => 'integer',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* Scope para filtrar productos activos
|
|
*/
|
|
public function scopeActivo(Builder $query): Builder
|
|
{
|
|
return $query->where('activo', true);
|
|
}
|
|
|
|
/**
|
|
* Scope para filtrar productos destacados
|
|
*/
|
|
public function scopeDestacado(Builder $query): Builder
|
|
{
|
|
return $query->where('destacado', true);
|
|
}
|
|
|
|
/**
|
|
* Scope para filtrar por categoría
|
|
*/
|
|
public function scopePorCategoria(Builder $query, string $categoria): Builder
|
|
{
|
|
return $query->where('categoria', $categoria);
|
|
}
|
|
|
|
/**
|
|
* Obtener precio formateado
|
|
*/
|
|
public function getPrecioFormateadoAttribute(): string
|
|
{
|
|
$currency = config('currency');
|
|
|
|
return $currency['symbol'].number_format($this->precio, $currency['decimal_places'], $currency['decimal_separator'], $currency['thousands_separator']);
|
|
}
|
|
|
|
/**
|
|
* Verificar si tiene imagen
|
|
*/
|
|
public function tieneImagen(): bool
|
|
{
|
|
return ! empty($this->imagen);
|
|
}
|
|
}
|