90 lines
2.2 KiB
PHP
90 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Product extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'makerworld_id',
|
|
'makerworld_collection_id',
|
|
'category_id',
|
|
'name',
|
|
'slug',
|
|
'short_description',
|
|
'description',
|
|
'base_price',
|
|
'sku',
|
|
'is_print_on_demand',
|
|
'stock',
|
|
'is_featured',
|
|
'is_active',
|
|
'status',
|
|
'dimensions',
|
|
'weight_grams',
|
|
'print_time_hours',
|
|
'print_info',
|
|
'image_url',
|
|
'makerworld_url',
|
|
'makerworld_author',
|
|
'makerworld_license',
|
|
'gallery_images',
|
|
'available_materials',
|
|
'available_colors',
|
|
];
|
|
|
|
protected $casts = [
|
|
'makerworld_id' => 'integer',
|
|
'base_price' => 'float',
|
|
'is_print_on_demand' => 'boolean',
|
|
'is_featured' => 'boolean',
|
|
'is_active' => 'boolean',
|
|
'print_time_hours' => 'float',
|
|
'print_info' => 'array',
|
|
'gallery_images' => 'array',
|
|
'available_materials' => 'array',
|
|
'available_colors' => 'array',
|
|
];
|
|
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function makerworldCollection(): BelongsTo
|
|
{
|
|
return $this->belongsTo(MakerWorldCollection::class, 'makerworld_collection_id');
|
|
}
|
|
|
|
public function orderItems(): HasMany
|
|
{
|
|
return $this->hasMany(OrderItem::class);
|
|
}
|
|
|
|
public function scopePublished(Builder $query): Builder
|
|
{
|
|
return $query->where('is_active', true)
|
|
->where(function ($q) {
|
|
$q->where('status', 'published')
|
|
->orWhereNull('status');
|
|
});
|
|
}
|
|
|
|
public function scopeDraft(Builder $query): Builder
|
|
{
|
|
return $query->where('status', 'draft');
|
|
}
|
|
|
|
public function getFormattedPriceAttribute(): string
|
|
{
|
|
return '€ '.number_format((float) ($this->base_price ?? 0), 2, ',', '.');
|
|
}
|
|
}
|