简体   繁体   English

如何在更新表中的列之前创建一个触发器,将整个旧行保存到新表中?

[英]How can I create a trigger that before updating a column in a table saves the entire old row into a new table?

I have the team_members table, with the following structure, where only project_request_id gets updated. 我有team_members表,具有以下结构,其中只有project_request_id得到更新。 I created log_team_members with the same data structure, so whenever I update team_members I lose the old data, which I need for reads, that's why I was thinking of creating a trigger that before updating team_members project_request_id , it saves the entire old row to this table. 我创建log_team_members具有相同数据结构的log_team_members ,所以每当我更新team_members时,我都会丢失旧数据,这就是我需要读取的内容,这就是为什么我想在创建trigger before updating team_members project_request_id ,它会将整个old row保存到此表中。

Can you help me to build this trigger from a Latavel migration? 你能帮我从Latavel迁移中构建这个触发器吗?

public function up()
{
    Schema::create('team_members', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('title');
        $table->string('picture');
        $table->string('email')->unique();
        $table->string('phone_number');
        $table->text('background_information')->nullable();
        $table->timestamps();
        $table->integer('project_request_id')->unsigned();
        $table->foreign('project_request_id')->references('id')->on('project_requests')->onDelete('cascade');
    });
}

public function up()
{
    Schema::create('team_members', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('title');
        $table->string('picture');
        $table->string('email')->unique();
        $table->string('phone_number');
        $table->text('background_information')->nullable();
        $table->timestamps();
        $table->integer('project_request_id')->unsigned();
        $table->foreign('project_request_id')->references('id')->on('project_requests')->onDelete('cascade');
    });
}

And this where the team_members update happens: 这就是team_members更新发生的地方:

public function createProjectTeam(Request $request){
            try {
                $projectRequest = 
ProjectRequest::create(['project_title' => $request->projectTitle, 
'client_id' => $request->projectClientId]);

                TeamMember::whereIn('email', $request->projectTeamEmails)
                ->update([
                    'project_request_id' => $projectRequest->id 
                ]);

                $projectTeam = TeamMember::where('project_request_id', $projectRequest->id)->get();

            return response()->json( [
                'success'=> true,
                'projectRequest' => $projectRequest,
                'projectTeam' => $projectTeam,
            ]);

            } catch(\Exception $e){
                return ['success' => false, 'message' => 'project team creation failed'];
            }

        }
// use DB; 
$data = UpdateModelName::find(1);
$copy = $data->replicate()->toArray();
// store data in new table
$newTable = new NewTableModel;
$newTable->insert($copy);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用触发器将列值更新为其他表中的旧值加上新值? - How can I Update column value to Old value plus New value from other table using Trigger? 创建触发器将旧数据插入新表 - Create a trigger to insert the old data to a new table 如何编写可以在插入时将表中的新行复制到另一个表的触发器? - How can I write a trigger that can copy new row from a table on insertion to another table? mysql触发器用于更新另一个表中的新行之后的表 - mysql trigger for updating a table after new row in another table 如何创建将新行添加到另一个表并将对新行的引用添加到当前表的触发器? - How to create a trigger that adds a new row to another table and a reference to the new row added to present table? 触发“更新前”触发器时,如何更新MySQL表行? - How can I update MySQL table row's when 'BEFORE UPDATE' trigger is firing? 创建触发器以在创建新行时将主键从一个表插入到 MySQL 中另一个表的列中 - Create trigger to insert primary key from one table upon creation of new row, into column of another table in MySQL 在更新MYSQL列之前触发在表中插入数据 - Trigger to Insert data in a table before updating a column MYSQL 如何在更新其他表时创建触发器 - How to create a trigger on updating other table 创建触发器以在MySQL的另一个表中插入新行时更新表 - create trigger to update a table on inserting new row in another table in MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM