63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
|
use Gloudemans\Shoppingcart\CanBeBought;
|
|
|
|
class Product extends Model implements Buyable
|
|
{
|
|
use HasFactory;
|
|
use SoftDeletes;
|
|
use CanBeBought;
|
|
|
|
//special formatted attribute price
|
|
public function getFormattedPriceAttribute()
|
|
{
|
|
return $this->attributes['formattedPrice'] = sprintf('%s €', number_format($this->attributes['price'], 2, ',', ' '));
|
|
}
|
|
|
|
public function range()
|
|
{
|
|
return $this->belongsTo('App\Models\Range', 'range_id');
|
|
}
|
|
|
|
public function old_slugs()
|
|
{
|
|
return $this->hasMany('App\Model\OldSlug', 'product_id');
|
|
}
|
|
|
|
public function stats()
|
|
{
|
|
return $this->hasMany('App\Models\Stat', 'product_id');
|
|
}
|
|
|
|
public function orders()
|
|
{
|
|
return $this->belongsToMany('App\Models\Order', 'orders_products', 'product_id', 'order_id')
|
|
->withPivot('quantity');
|
|
}
|
|
|
|
//relationship to additionnal product info
|
|
public function additionnalProductInfo($productType)
|
|
{
|
|
return $this->$productType;
|
|
}
|
|
|
|
//possible additionnals
|
|
public function foodProduct()
|
|
{
|
|
return $this->hasOne(FoodProduct::class, 'product_id');
|
|
}
|
|
|
|
|
|
|
|
//relationship to associated products
|
|
public function associated_products()
|
|
{
|
|
return $this->belongsToMany(Product::class, 'products_products', 'product_id', 'associated_id');
|
|
}
|
|
}
|