1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- /**
- * 购物车模型
- *
- */
- class ShopCart extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'shop_cart';
- // 是否主动维护时间戳
- 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);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$uid,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where([['id','=',$id],['custom_uid','=',$uid]])->update($data);
- // 返回结果
- return $result;
- }
- /**
- * 添加数据
- *
- */
- public function del($id,$uid)
- {
- // 写入数据表
- $result = $this->query()->where([['id','=',$id],['custom_uid','=',$uid]])->delete();
- // 返回结果
- return $result;
- }
- /**
- * 数量更新
- *
- * @param int $id 购物车ID
- * @param int $uid 用户ID
- * @param int $incr 增减数量
- */
- public function incrBuyNum($id,$uid,$incr=1){
- // 返回结果
- return $this->query()->where([['id','=',$id],['custom_uid','=',$uid]])->update(['buy_num'=>DB::raw('buy_num+'.$incr),'update_time'=>time()]);
- }
- }
|