1. Go to this page and download the library: Download m-sonmez/laravel-triggers library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
m-sonmez / laravel-triggers example snippets
use Msonmez\LaravelTriggers\Enums\TriggerTiming;
use Msonmez\LaravelTriggers\Enums\TriggerEvent;
use Msonmez\LaravelTriggers\Helpers\Trigger;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('accounts', function (Blueprint $table) {
$table->trigger(
name: 'LogAccountCreation',
timing: TriggerTiming::AFTER,
event: TriggerEvent::INSERT,
statements: Trigger::insert('actions', [
'type' => 1,
'table_name' => 'accounts',
'record_id' => Trigger::new('id'),
'created_at' => Trigger::now(),
])
);
});
Schema::table('accounts', function (Blueprint $table) {
$table->dropTrigger('LogAccountCreation');
});
Schema::table('accounts', function (Blueprint $table) {
if ($table->hasTrigger('LogAccountCreation')) {
// do operations based on the existence of a trigger
}
});
$table->trigger(
name: 'CheckUserBalance',
timing: TriggerTiming::BEFORE,
event: TriggerEvent::INSERT,
condition: '(SELECT balance FROM users WHERE id = NEW.user_id) < NEW.amount',
statements: Trigger::abort('Insufficient balance for this transaction')
);