[英]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.