123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php namespace App\Models\Product;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 产品属性
- *
- */
- class Attr extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'product_attr';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 如果操作失败
- if( !$id ) return $id;
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where([['id','=',$id]])->update($data);
- // 如果操作失败
- if( !$result ) return $result;
- // 返回结果
- return $result;
- }
- /**
- * 添加数据
- *
- */
- public function upsertByName($name,$productId,$specId)
- {
- // 写入数据表
- $id = $this->query()->where([['name','=',$name],['product_id','=',$productId],['spec_id','=',$specId]])->value('id');
- // 如果操作失败
- if( !$id ) return $this->add(['name'=>$name,'product_id'=>$productId,'spec_id'=>$specId]);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function getListByProductId($productId)
- {
- // 写入数据表
- $list = $this->query()->where([['product_id','=',$productId]])->get(['id','name','spec_id','product_id','thumb','remark'])->toArray();
- // 返回结果
- return $list;
- }
- /**
- * 添加数据
- *
- */
- public function getValueById($id,$field='')
- {
- // 写入数据表
- $result = $this->query()->where([['id','=',$id]])->value($field);
- // 返回结果
- return $result;
- }
- }
|