Create trigger generate id for estimate in Laravel 9

Create trigger generate id for estimate in Laravel 9

   Create trigger generate ID for an estimate in Laravel 9 HR System 


Purpose

Use the CREATE TRIGGER statement to create and enable a database trigger, which is:

  • A stored PL/SQL block associated with a table, a schema, or the database or

  • An anonymous PL/SQL block or a call to a procedure implemented in PL/SQL or Java

Oracle Database automatically executes a trigger when specified conditions occur.

When you create a trigger, the database enables it automatically. You can subsequently disable and enable a trigger with the DISABLE and ENABLE clause of the ALTER TRIGGER or ALTER TABLE statement.


Step 1:Create Migration:

Using the below command you can simply create migration for a database table.

php artisan make:migration sequence_estimate_number_table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sequence_tbls', function (Blueprint $table) {
$table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('sequence_tbls');
} };

Step 2:Create Migration:

Using the below command you can simply create migration for a database table.

php artisan make:migration create_estimate_numbers_table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::unprepared('
            CREATE TRIGGER id_estimate BEFORE INSERT ON estimates FOR EACH ROW
            BEGIN
                INSERT INTO sequence_tbls VALUES (NULL);
SET NEW.estimate_number = CONCAT("EST_", LPAD(LAST_INSERT_ID(), 6, "0")); END '); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP TRIGGER "id_estimate"'); } };

Step 3:Create Migration:

Using the below command you can simply create migration for a database table.

php artisan make:migration estimates_table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('estimates', function (Blueprint $table) {
            $table->id();
            $table->string('estimate_number');
            $table->string('project')->nullable();
            $table->string('email')->nullable();
            $table->string('tax')->nullable();
            $table->string('client_address')->nullable();
            $table->string('billing_address')->nullable();
            $table->string('estimate_date')->nullable();
            $table->string('expiry_date')->nullable();
            $table->string('total')->nullable();
            $table->string('tax_1')->nullable();
            $table->string('discount')->nullable();
            $table->string('grand_total')->nullable();
            $table->string('other_information')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('estimates');
    }
};

Step 4:Run

After adding the run file now run the migrate command.

php artisan serve

StarCode Kh

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

Previous Post Next Post
close