You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							78 lines
						
					
					
						
							1.6 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							78 lines
						
					
					
						
							1.6 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Models;
							 | 
						|
								
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Jobs\ProductSaved;
							 | 
						|
								use Illuminate\Database\Eloquent\Factories\HasFactory;
							 | 
						|
								use Illuminate\Database\Eloquent\SoftDeletes;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 供应商产品
							 | 
						|
								 * Class Product
							 | 
						|
								 * @package App\Models
							 | 
						|
								 */
							 | 
						|
								class Product extends BaseModel
							 | 
						|
								{
							 | 
						|
								    use HasFactory, SoftDeletes;
							 | 
						|
								
							 | 
						|
									protected $guarded = ['id']; //不允许编辑的字段
							 | 
						|
									protected $casts = ['extends' => 'json'];
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * 模型的 "booted" 方法
							 | 
						|
									 * @return void
							 | 
						|
									 */
							 | 
						|
									protected static function booted()
							 | 
						|
									{
							 | 
						|
										parent::booted();
							 | 
						|
										static::saved(function ($product) {
							 | 
						|
											if ($product->isDirty('status') && $product->status == ProductStatus::ON_SALE) {
							 | 
						|
												ProductSaved::dispatch($product);
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									// 获取所有产品图片
							 | 
						|
								    public function getPicturesAttribute($value): array
							 | 
						|
								    {
							 | 
						|
								    	if (is_string($value)) {
							 | 
						|
											$value = $value ? json_decode($value, true) : [];
							 | 
						|
										}
							 | 
						|
								        $this->append('picture');
							 | 
						|
								        return $value ?? [];
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    // 获取第一张产品图片
							 | 
						|
								    public function getPictureAttribute($value): string
							 | 
						|
								    {
							 | 
						|
								        return $this->pictures[0] ?? '';
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
									public function setPicturesAttribute($value)
							 | 
						|
									{
							 | 
						|
										if (is_array($value)) {
							 | 
						|
											$this->attributes['pictures'] = json_encode(array_filter($value));
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function supplier()
							 | 
						|
									{
							 | 
						|
										return $this->belongsTo(Supplier::class)->withTrashed();
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function category()
							 | 
						|
									{
							 | 
						|
										return $this->belongsTo(Category::class);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function spec()
							 | 
						|
									{
							 | 
						|
										return $this->hasMany(ProductSpec::class);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function diyForm()
							 | 
						|
									{
							 | 
						|
										return $this->belongsTo(DiyForm::class);
							 | 
						|
									}
							 | 
						|
								}
							 |