一、表结构
CREATE TABLE `apply_device_manage` (
`id` int NOT NULL AUTO_INCREMENT,
`device_type` int DEFAULT NULL COMMENT '设备类型:1 手机 2移动设备 3其他',
`device_id` int DEFAULT NULL COMMENT '设备id',
`classfy_id` int DEFAULT NULL COMMENT '关联的分类ID',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '备注',
`apply_no` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '申请编号',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='设备申请';
CREATE TABLE `phone_device` (
`id` int NOT NULL AUTO_INCREMENT,
`name` int DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='手机';
CREATE TABLE `phone_classfy` (
`id` int NOT NULL AUTO_INCREMENT,
`name` int DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='分类';
CREATE TABLE `move_device` (
`id` int NOT NULL AUTO_INCREMENT,
`name` int DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='移动设备';
CREATE TABLE `move_classfy` (
`id` int NOT NULL AUTO_INCREMENT,
`name` int DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='移动设备分类';
CREATE TABLE `other_device` (
`id` int NOT NULL AUTO_INCREMENT,
`name` int DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='其他设备';
CREATE TABLE `other_classfy` (
`id` int NOT NULL AUTO_INCREMENT,
`name` int DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='其他设备分类';
二 laravel 模型编写
1、编写ApplyDeviceManage的model
class ApplyDeviceManage
{
protected $table = 'apply_device_manage';
public $timestamps = true;
protected $guarded = ['id'];
protected static function booted()
{
parent::booted();
self::retrieved(function (DeviceManage $comment) {
$comment->attributes['device_type_class'] = match ((int) $comment->device_type) {
1 => PhoneDevice::class,
3 => CaseGeneral::class,
4 => Community::class,
default => $comment->device_type,
};
$comment->makeHidden(['device_type_class']);
$comment->attributes['project_type_class'] = match ((int) $comment->device_type) {
1 => PhoneClassfy::class,
3 => TenderGeneral::class,
4 => Tender::class,
default => $comment->device_type,
};
$comment->makeHidden(['project_type_class']);
});
self::saving(function (DeviceManage $comment) {
unset($comment->attributes['device_type_class']);
unset($comment->attributes['project_type_class']);
});
}
public function device()
{
return $this->morphTo('device', 'device_type_class', 'device_id','id');
}
public function classfy()
{
return $this->morphTo('classfy', 'project_type_class', 'classfy_id','id');
}
}
2、编写PhoneDevice的model
class PhoneDevice
{
protected $table = 'phone_device';
public $timestamps = true;
protected $guarded = ['id'];
public function applyDeviceManages()
{
return $this->morphOne(ApplyDeviceManage ::class, 'device');
}
}
3、编写PhoneClassfy的model
class PhoneClassfy
{
protected $table = 'phone_classfy';
public $timestamps = true;
protected $guarded = ['id'];
public function applyDeviceManages()
{
return $this->morphOne(ApplyDeviceManage ::class, 'classfy');
}
}
4、编写MoveDevice的model
class MoveDevice
{
protected $table = 'move_device';
public $timestamps = true;
protected $guarded = ['id'];
public function applyDeviceManages()
{
return $this->morphOne(ApplyDeviceManage ::class, 'device');
}
}
5、编写MoveClassfy的model
class MoveClassfy
{
protected $table = 'move_classfy';
public $timestamps = true;
protected $guarded = ['id'];
public function applyDeviceManages()
{
return $this->morphOne(ApplyDeviceManage ::class, 'classfy');
}
}
6、编写MoveClassfy的model
class OtherDevice
{
protected $table = 'other_device';
public $timestamps = true;
protected $guarded = ['id'];
public function applyDeviceManages()
{
return $this->morphOne(ApplyDeviceManage ::class, 'device');
}
}
7、编写MoveClassfy的model
class OtherClassfy
{
protected $table = 'other_classfy';
public $timestamps = true;
protected $guarded = ['id'];
public function applyDeviceManages()
{
return $this->morphOne(ApplyDeviceManage ::class, 'classfy');
}
}
8、编写Controller
class ApplyDeviceManageController
{
public function list(Request $request)
{
$param = $request->all();
$query = ApplyDeviceManage ::query()
->with(['device','classfy',])
;
$list = $query->paginate($param['limit']);
return $list;
}
public function detail(Request $request)
{
$id = $request->input('id');
$query = ApplyDeviceManage ::query()
->with(['device','classfy',])
;
$info = $query->find($id);
return $info;
}
}
以上操作就可以保证通过 device_type 的类型,可以动态关联到不同的模型,查询到不同的数据。
在lavarel10 测试是可以行的。