ldc-legaulois/app/Models/Product.php
2024-04-19 11:37:53 +02:00

132 lines
3.9 KiB
PHP

<?php
namespace App\Models;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Facades\Vite;
/**
* @property string $label Product label.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Product extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'label',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $productModel) {
});
static::updating(function (self $productModel) {
});
static::updated(function (self $productModel) {
});
static::deleted(function (self $productModel) {
});
}
// * METHODS
/**
* Display a listing of the resource.
*
* @param string|null $slug
* @return array<string>
*/
public static function getProductsViewsByRange(string|null $slug = null): array
{
/** FIXME: replace 'first-range' by the first Range slug publicated with order by ASC. */
$rangeSlug = !is_null($slug) ? $slug : "first-range";
/** FIXME: replace this part of 'productModels' values by products associated to the range (limit 6, random). */
$productModels = [];
for ($i = 0; $i < 5; $i++) {
$productModels[] = (object)[
"name" => "Produit $i de la gamme $rangeSlug",
'packaging' => Vite::asset('resources/assets/images/tmp/packaging.png'),
'packaging_alt' => "lorem ipsum",
'packaging_title' => "lorem ipsum",
'picture' => Vite::asset('resources/assets/images/tmp/product-picture.png'),
'picture_alt' => "lorem ipsum",
'picture_title' => "lorem ipsum",
'tag' => Vite::asset('resources/assets/images/tmp/product-tag.png'),
'tag_alt' => "lorem ipsum",
'tag_title' => "lorem ipsum",
'isFavorite' => ($i % 2) ? true : false,
'isNew' => ($i % 2) ? false : true,
];
}
foreach ($productModels as $productModel) {
$productViews[] = view('fo.partials.product-card', compact('productModel'))->render();
} //end foreach
return $productViews;
}
// * RELATIONSHIPS
/**
* Get all of the ranges that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function ranges(): MorphToMany
{
return $this->morphedByMany(Range::class, 'productable');
}
/**
* Get all of the subranges that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function subranges(): MorphToMany
{
return $this->morphedByMany(Subrange::class, 'productable');
}
/**
* Get all of the meats that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function meats(): MorphToMany
{
return $this->morphedByMany(Meat::class, 'productable');
}
/**
* Get all of the submeats that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function submeats(): MorphToMany
{
return $this->morphedByMany(Submeat::class, 'productable');
}
}