Skip to content

Commit 709d2bc

Browse files
committed
add trigger count total rab
1 parent 2824204 commit 709d2bc

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
5+
class CreateTriggerCountAfterInsert extends Migration
6+
{
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
DB::unprepared('CREATE TRIGGER `count_after_insert` AFTER INSERT ON `rab_details` FOR EACH ROW BEGIN set @total =(SELECT SUM(`total`) FROM `rab_details` WHERE `rab_id` = new.rab_id); UPDATE `rabs` SET `total` = @total WHERE `id` = new.rab_id; END');
15+
}
16+
17+
/**
18+
* Reverse the migrations.
19+
*
20+
* @return void
21+
*/
22+
public function down()
23+
{
24+
DB::unprepared('DROP TRIGGER `count_after_insert`');
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
5+
class CreateTriggerCountAfterUpdate extends Migration
6+
{
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
DB::unprepared('CREATE TRIGGER `count_after_update` AFTER UPDATE ON `rab_details` FOR EACH ROW BEGIN set @total =(SELECT SUM(`total`) FROM `rab_details` WHERE `rab_id` = old.rab_id); UPDATE `rabs` SET `total` = @total WHERE `id` = old.rab_id; END');
15+
}
16+
17+
/**
18+
* Reverse the migrations.
19+
*
20+
* @return void
21+
*/
22+
public function down()
23+
{
24+
DB::unprepared('DROP TRIGGER `count_after_update`');
25+
}
26+
}

0 commit comments

Comments
 (0)