Files
ryobi-crawler/src/Models/Product.php

63 lines
1.4 KiB
PHP

<?php
namespace Krzysiej\RyobiCrawler\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* @property integer $skuID
* @property string $name
* @property integer $availableQuantity
* @property integer $stock
* @property string[] $categories
* @property string $image
* @property string $subTitle
* @property string $variantCode
* @property string $modelCode
* @property string $url
* @property int $starred
*/
class Product extends Model
{
public $timestamps = true;
public $fillable = ['skuID'];
public function price(): HasMany
{
return $this->hasMany(Price::class);
}
public function isStarred(): bool
{
return (bool) $this->starred;
}
public function currentPrice(): HasOne
{
return $this->hasOne(Price::class)->latestOfMany('created_at');
}
public function stock(): HasMany
{
return $this->hasMany(Stock::class);
}
public function toggleStarred(): self
{
$this->starred = !$this->starred;
return $this;
}
public function categories(): Attribute
{
return Attribute::make(
get: fn(string $value) => array_reverse(json_decode($value, 1)),
set: fn(array $value) => json_encode($value),
);
}
}