Laravel10动态关联模型

一、表结构

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 测试是可以行的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗼唸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值