sineld
<?php
class Product extends Model
{
use SoftDeletes;
protected $table = 'products';
protected $fillable = [
'id',
'code',
'name',
'category_id',
'description',
'image',
'commission',
'price',
'buy_now',
'route',
'btn_text',
'type',
'status',
'short_desc',
'pricing_desc',
'author',
'author_email',
'author_web',
'author_logo',
'addon_id',
'show',
'discount_code_status',
'discount',
'discount_status',
'offer_status',
'period_status',
'period',
'period_frequency',
'sort',
'remaning_status',
'action',
'subscription',
'key',
'endpoint',
'product_type',
'unique_id',
'discount_show_status',
'annual_payment',
'auto_assign_user_id',
'relational_product_status',
'supplier_id',
'form_id',
'contents',
'auto_invoice',
'pre_register',
'privacy_status',
'privacy',
'sg_crm_code',
'sg_supplier',
'sg_project',
'activated_at',
'passived_at',
'last_pre_register_date',
'supplier_commision',
'popup_show',
'earn_money',
'product_desc',
'start_popup_time',
'finish_popup_time',
];
protected $hidden = [
'auto_assign_user_id'
];
protected $dates = [];
protected $casts = [
'contents' => 'array'
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if($model->category_id) {
$category = ProductCategory::find($model->category_id);
if($category) {
$model->type = $category->name;
}
}
$model->code = initials($model->name) . '01';
});
static::updating(function ($model) {
if($model->category_id) {
$category = ProductCategory::find($model->category_id);
if($category) {
$model->type = $category->name;
}
}
});
// static::addGlobalScope(new MyProductScope);
}
public function scopeMyProduct($query)
{
$user = auth()->user();
if ($user && !$user->isAdmin() && $user->role != 'business') {
$ids = DB::table('user_products')->where('user_id', $user->id)->pluck('product_id');
$query->whereIn('products.id', $ids);
}
}
protected $appends = [
'image_url',
'author_logo_url',
'data',
'education',
'unique',
'description_string'
];
protected function castAttribute($key, $value)
{
if ($this->getCastType($key) == 'array' && is_null($value)) {
return [];
}
return parent::castAttribute($key, $value);
}
public function getContentsAttribute($value)
{
if(is_null($value)) {
return [];
}
try {
$data = array_values(json_decode($value, true));
$data = array_filter($data, function($value) {
return !is_null(Arr::get($value, 'title')) && $value !== '';
});
return $data;
} catch (\Exception $th) {
return [];
}
}
public function getAuthorLogoUrlAttribute()
{
return $this->author_logo ? url($this->author_logo) : null;
return Storage::disk('local')->get(public_path($this->author_logo));
if(!$this->author_logo) {
return null;
}
$exists = Storage::disk('s3')->exists($this->author_logo);
if(!$exists) {
return s3Url($this->author_logo);
}
return $upload = Storage::disk('s3')->putFileAs(
'suppliers',
Storage::get(public_path($this->author_logo)),
str_replace('suppliers/', '', $this->author_logo)
);
}
public function getImageUrlAttribute()
{
if(file_exists(public_path($this->image))) {
return $this->image;
}
try {
return s3Url($this->image);
} catch (\Exception $e) {
return null;
}
}
public function getUniqueAttribute()
{
if (empty($this->unique_id) && is_null($this->unique_id)) {
$date = $this->created_at->format('md');
$this->update([
'unique_id' => $date . $this->id,
]);
}
return $this->unique_id;
}
public function getDataAttribute()
{
if ($this->code == 1) {
return [
'urlKey' => auth()->user()->mikrox_url_key,
];
}
return false;
}
public function getEducationAttribute()
{
if ($this->product_type == 'online_education') {
return ProductPriceEducation::whereIn('product_price_id', $this->prices()->pluck('id'))
->whereDate('date', '>=', Carbon::now()->toDateString())
->orderBy('date', 'DESC')
->first();
}
return null;
}