我正在尝试为我的模型添加一个新的特性或其他特性
我的数据库mysql中有一些关系,我使用的是laravel 5.6
我有一些包含'is_approved‘列的表
现在我想创建一些东西,当我将我的一个表的is_approved设置为false时,与该表有关系的所有其他表都会更新为我想要选择的值。
我该怎么做呢?
例如:
我有这些表格:
-product_type [columns: 'id', 'type_name', 'is_approved']
-product_brand [columns: 'id', 'type_id', 'brand_name', 'is_approved']
-product_model [columns: 'id', 'brand_id', 'model_name', 'is_approved']
-product_name [columns: 'id', 'model_id', 'product_name', 'is_approved']并且所有这些表都有'is_approved‘列
我想,当我将其中一个product_type记录'is_approved‘列设置为false时,所有I关系记录都更新为false
发布于 2019-03-05 17:45:51
你可以在你的模型上使用Events。
发布于 2019-03-05 17:49:14
若要将列更新到其他表,请执行以下操作。对于type_id,您需要手动创建它。如下所示:
$productbrand = ProductBrand::where('type_id', $type_id)->update(['is_approved' => 1]);
$productmodel = ProductModel::where('brand_id', $productbrand->id)->update(['is_approved' => 1]);
$productname = ProductName::where('model_id', $productmodel->id)->update(['is_approved' => 1]);或者,您可以使用触发器来完成此操作。有关更多详细信息,请阅读此处。
http://www.expertphp.in/article/laravel-53-creating-mysql-triggers-from-migration-with-example
发布于 2019-03-05 18:00:08
您可以使用ON UPDATE CASCADE属性将is_approved列作为所有外键的一部分:
create table product_type(
id int auto_increment primary key,
type_name varchar(50),
is_approved tinyint,
index(id, is_approved)
);
create table product_brand (
id int auto_increment primary key,
type_id int,
brand_name varchar(50),
is_approved tinyint,
index(id, is_approved),
foreign key (type_id, is_approved)
references product_type(id, is_approved)
on update cascade
);
create table product_model (
id int auto_increment primary key,
brand_id int,
model_name varchar(50),
is_approved tinyint,
index(id, is_approved),
foreign key (brand_id, is_approved)
references product_brand(id, is_approved)
on update cascade
);
create table product_name (
id int auto_increment primary key,
model_id int,
product_name varchar(50),
is_approved tinyint,
foreign key (model_id, is_approved)
references product_model(id, is_approved)
on update cascade
);product_type中的更改将自动更改product_brand中的相应行。product_brand中的更改将导致product_model中的更改。product_model中的更改将导致product_name中的更改。
https://stackoverflow.com/questions/54999624
复制相似问题