ldc-boutique-cnc/app/Models/Product.php

64 lines
1.6 KiB
PHP
Raw Normal View History

2020-11-05 17:08:32 +01:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2020-11-17 13:41:31 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
2021-03-24 17:58:27 +01:00
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\CanBeBought;
2020-11-05 17:08:32 +01:00
2021-03-24 17:58:27 +01:00
class Product extends Model implements Buyable
2020-11-05 17:08:32 +01:00
{
use HasFactory;
2020-11-17 13:41:31 +01:00
use SoftDeletes;
2021-03-24 17:58:27 +01:00
use CanBeBought;
2020-11-05 17:08:32 +01:00
2021-03-16 17:48:35 +01:00
//special formatted attribute price
public function getFormattedPriceAttribute()
{
return $this->attributes['formattedPrice'] = sprintf('%s €', number_format($this->attributes['price'], 2, ',', ' '));
}
2021-03-18 08:40:20 +01:00
public function range()
{
return $this->belongsTo('App\Models\Range', 'range_id');
2020-11-05 17:08:32 +01:00
}
2021-03-18 08:40:20 +01:00
public function old_slugs()
{
2020-11-05 17:08:32 +01:00
return $this->hasMany('App\Model\OldSlug', 'product_id');
}
2020-11-17 13:41:31 +01:00
2021-03-18 08:40:20 +01:00
public function stats()
{
return $this->hasMany('App\Models\Stat', 'product_id');
2020-11-17 13:41:31 +01:00
}
2021-03-18 08:40:20 +01:00
public function orders()
{
return $this->belongsToMany('App\Models\Order', 'orders_products', 'product_id', 'order_id')
->withPivot('quantity');
2020-11-17 13:41:31 +01:00
}
2021-03-16 17:48:35 +01:00
//relationship to additionnal product info
2021-03-18 08:40:20 +01:00
public function additionnalProductInfo($productType)
{
return $this->$productType;
2021-03-16 17:48:35 +01:00
}
2021-03-18 08:40:20 +01:00
//possible additionnals
public function foodProduct()
{
return $this->hasOne(FoodProduct::class, 'product_id');
}
2021-03-16 17:48:35 +01:00
//relationship to associated products
2021-03-18 08:40:20 +01:00
public function associated_products()
{
return $this->belongsToMany(Product::class, 'products_products', 'product_id', 'associated_id');
2021-03-16 17:48:35 +01:00
}
2020-11-05 17:08:32 +01:00
}