团购后台解决方案及代码实现
一、数据表结构
商品表:
CREATE TABLE `goods` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`goods_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`goods_price` decimal(10, 2) NULL DEFAULT NULL,
`goods_stock` int(5) NULL DEFAULT NULL,
`goods_no` int(10) NULL DEFAULT NULL,
`is_pt` int(1) NULL DEFAULT NULL,
`deleted_at` timestamp(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
团购表:
CREATE TABLE `pt_goods` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`pt_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`goods_id` int(10) NULL DEFAULT NULL,
`pt_price` decimal(10, 2) NULL DEFAULT NULL,
`start_time` datetime(0) NULL DEFAULT NULL,
`end_time` datetime(0) NULL DEFAULT NULL,
`status` int(1) NULL DEFAULT NULL,
`order` int(6) NULL DEFAULT NULL,
`pt_num` int(1) NULL DEFAULT NULL,
`deleted_at` timestamp(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
二、模型实现
运行下面命令创建模型
php artisan make:model Pt
模型代码
class Pt extends Model
{
//软删除
use SoftDeletes;
protected $table='自己创建的表';
//关联商品表
public function goods(){
return $this->belongsTo(Goods::class,'goods_id','id');
}
//根据活动时间 修改活动状态
public function getStatusAttribute($status)
{
if ($status == 3) {
return 3;
}elseif ($this->start_time > date('Y-m-d H:i:s')){
if ($status!=0){
$this->status=<