How to use triggers in laravel?2019 Community Moderator ElectionHow does database indexing work?How can I prevent SQL injection in PHP?How do I quickly rename a MySQL database (change schema name)?How to get a list of MySQL user accountsHow to trigger event in JavaScript?Laravel 4 database statement create triggerHow to import an SQL file using the command line in MySQL?can't create a trigger in mysqlMysql prevent select on a table while executing a triggerMySQL trigger Error: 1442

What will happen if my luggage gets delayed?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Help! My Character is too much for her story!

Is it appropriate to ask a former professor to order a book for me through an inter-library loan?

Is there stress on two letters on the word стоят

What is this tube in a jet engine's air intake?

I can't die. Who am I?

Create chunks from an array

What do you call someone who likes to pick fights?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Should we avoid writing fiction about historical events without extensive research?

How to educate team mate to take screenshots for bugs with out unwanted stuff

Is this Paypal Github SDK reference really a dangerous site?

How to install round brake pads

Is there a way to make cleveref distinguish two environments with the same counter?

Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?

Sampling from Gaussian mixture models, when are the sampled data independent?

(Codewars) Linked Lists-Sorted Insert

What is Tony Stark injecting into himself in Iron Man 3?

Are all players supposed to be able to see each others' character sheets?

-1 to the power of a irrational number

Either of .... (Plural/Singular)

The (Easy) Road to Code

Which country has more?



How to use triggers in laravel?



2019 Community Moderator ElectionHow does database indexing work?How can I prevent SQL injection in PHP?How do I quickly rename a MySQL database (change schema name)?How to get a list of MySQL user accountsHow to trigger event in JavaScript?Laravel 4 database statement create triggerHow to import an SQL file using the command line in MySQL?can't create a trigger in mysqlMysql prevent select on a table while executing a triggerMySQL trigger Error: 1442










0















My code, using PHP artisan make: migration create_trigger command



public function up()

DB::unprepared('
CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
BEGIN
SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE
class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
NEW.student_roll_no = CONCAT(
YEAR(CURRENT_DATE),
NEW.class_code,
IF (@roll_num < 10, CONCAT(`0`, @roll_num), @roll_num)
)
END;
');










share|improve this question









New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Welcome to SO! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – A Jar of Clay
    Mar 6 at 13:19















0















My code, using PHP artisan make: migration create_trigger command



public function up()

DB::unprepared('
CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
BEGIN
SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE
class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
NEW.student_roll_no = CONCAT(
YEAR(CURRENT_DATE),
NEW.class_code,
IF (@roll_num < 10, CONCAT(`0`, @roll_num), @roll_num)
)
END;
');










share|improve this question









New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Welcome to SO! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – A Jar of Clay
    Mar 6 at 13:19













0












0








0








My code, using PHP artisan make: migration create_trigger command



public function up()

DB::unprepared('
CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
BEGIN
SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE
class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
NEW.student_roll_no = CONCAT(
YEAR(CURRENT_DATE),
NEW.class_code,
IF (@roll_num < 10, CONCAT(`0`, @roll_num), @roll_num)
)
END;
');










share|improve this question









New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












My code, using PHP artisan make: migration create_trigger command



public function up()

DB::unprepared('
CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
BEGIN
SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE
class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
NEW.student_roll_no = CONCAT(
YEAR(CURRENT_DATE),
NEW.class_code,
IF (@roll_num < 10, CONCAT(`0`, @roll_num), @roll_num)
)
END;
');







mysql database laravel triggers laravel-5.7






share|improve this question









New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 6 at 13:35









barbsan

2,45041323




2,45041323






New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 6 at 12:35









HARIKRISHNAHARIKRISHNA

51




51




New contributor




HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






HARIKRISHNA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Welcome to SO! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – A Jar of Clay
    Mar 6 at 13:19

















  • Welcome to SO! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – A Jar of Clay
    Mar 6 at 13:19
















Welcome to SO! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

– A Jar of Clay
Mar 6 at 13:19





Welcome to SO! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

– A Jar of Clay
Mar 6 at 13:19












2 Answers
2






active

oldest

votes


















1














You don't need to create a migration for a model event. Laravel eloquent has multiple events such as retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored that you can easily use them.



first, you should create Observer for your model like this



php artisan make:observer UserObserver --model=User



in the UserObserver you can listen to any event that you like such as:



class UserObserver

/**
* Handle the User "created" event.
*
* @param AppUser $user
* @return void
*/
public function created(User $user)

//


/**
* Handle the User "updated" event.
*
* @param AppUser $user
* @return void
*/
public function updated(User $user)

//




after that you should register your observer to model in app/providers/AppServiceProvider boot method such as:



public function boot()

User::observe(UserObserver::class);




for more detail visit Laravel documentation.






share|improve this answer






























    0














    try this:
    please check your SQL syntax



    DB::unprepared('CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
    BEGIN
    SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
    NEW.student_roll_no = CONCAT(YEAR(CURRENT_DATE)),
    NEW.class_code,
    IF (@roll_num < 10,
    CONCAT(`0`, @roll_num),
    @roll_num
    )
    END');


    for example please check this link :



    [https://itsolutionstuff.com/post/how-to-add-mysql-trigger-from-migrations-in-laravel-5example.html]1



    i hope help you






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      HARIKRISHNA is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023271%2fhow-to-use-triggers-in-laravel%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You don't need to create a migration for a model event. Laravel eloquent has multiple events such as retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored that you can easily use them.



      first, you should create Observer for your model like this



      php artisan make:observer UserObserver --model=User



      in the UserObserver you can listen to any event that you like such as:



      class UserObserver

      /**
      * Handle the User "created" event.
      *
      * @param AppUser $user
      * @return void
      */
      public function created(User $user)

      //


      /**
      * Handle the User "updated" event.
      *
      * @param AppUser $user
      * @return void
      */
      public function updated(User $user)

      //




      after that you should register your observer to model in app/providers/AppServiceProvider boot method such as:



      public function boot()

      User::observe(UserObserver::class);




      for more detail visit Laravel documentation.






      share|improve this answer



























        1














        You don't need to create a migration for a model event. Laravel eloquent has multiple events such as retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored that you can easily use them.



        first, you should create Observer for your model like this



        php artisan make:observer UserObserver --model=User



        in the UserObserver you can listen to any event that you like such as:



        class UserObserver

        /**
        * Handle the User "created" event.
        *
        * @param AppUser $user
        * @return void
        */
        public function created(User $user)

        //


        /**
        * Handle the User "updated" event.
        *
        * @param AppUser $user
        * @return void
        */
        public function updated(User $user)

        //




        after that you should register your observer to model in app/providers/AppServiceProvider boot method such as:



        public function boot()

        User::observe(UserObserver::class);




        for more detail visit Laravel documentation.






        share|improve this answer

























          1












          1








          1







          You don't need to create a migration for a model event. Laravel eloquent has multiple events such as retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored that you can easily use them.



          first, you should create Observer for your model like this



          php artisan make:observer UserObserver --model=User



          in the UserObserver you can listen to any event that you like such as:



          class UserObserver

          /**
          * Handle the User "created" event.
          *
          * @param AppUser $user
          * @return void
          */
          public function created(User $user)

          //


          /**
          * Handle the User "updated" event.
          *
          * @param AppUser $user
          * @return void
          */
          public function updated(User $user)

          //




          after that you should register your observer to model in app/providers/AppServiceProvider boot method such as:



          public function boot()

          User::observe(UserObserver::class);




          for more detail visit Laravel documentation.






          share|improve this answer













          You don't need to create a migration for a model event. Laravel eloquent has multiple events such as retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored that you can easily use them.



          first, you should create Observer for your model like this



          php artisan make:observer UserObserver --model=User



          in the UserObserver you can listen to any event that you like such as:



          class UserObserver

          /**
          * Handle the User "created" event.
          *
          * @param AppUser $user
          * @return void
          */
          public function created(User $user)

          //


          /**
          * Handle the User "updated" event.
          *
          * @param AppUser $user
          * @return void
          */
          public function updated(User $user)

          //




          after that you should register your observer to model in app/providers/AppServiceProvider boot method such as:



          public function boot()

          User::observe(UserObserver::class);




          for more detail visit Laravel documentation.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 6 at 13:15









          hoseinz3hoseinz3

          1479




          1479























              0














              try this:
              please check your SQL syntax



              DB::unprepared('CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
              BEGIN
              SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
              NEW.student_roll_no = CONCAT(YEAR(CURRENT_DATE)),
              NEW.class_code,
              IF (@roll_num < 10,
              CONCAT(`0`, @roll_num),
              @roll_num
              )
              END');


              for example please check this link :



              [https://itsolutionstuff.com/post/how-to-add-mysql-trigger-from-migrations-in-laravel-5example.html]1



              i hope help you






              share|improve this answer



























                0














                try this:
                please check your SQL syntax



                DB::unprepared('CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
                BEGIN
                SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
                NEW.student_roll_no = CONCAT(YEAR(CURRENT_DATE)),
                NEW.class_code,
                IF (@roll_num < 10,
                CONCAT(`0`, @roll_num),
                @roll_num
                )
                END');


                for example please check this link :



                [https://itsolutionstuff.com/post/how-to-add-mysql-trigger-from-migrations-in-laravel-5example.html]1



                i hope help you






                share|improve this answer

























                  0












                  0








                  0







                  try this:
                  please check your SQL syntax



                  DB::unprepared('CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
                  BEGIN
                  SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
                  NEW.student_roll_no = CONCAT(YEAR(CURRENT_DATE)),
                  NEW.class_code,
                  IF (@roll_num < 10,
                  CONCAT(`0`, @roll_num),
                  @roll_num
                  )
                  END');


                  for example please check this link :



                  [https://itsolutionstuff.com/post/how-to-add-mysql-trigger-from-migrations-in-laravel-5example.html]1



                  i hope help you






                  share|improve this answer













                  try this:
                  please check your SQL syntax



                  DB::unprepared('CREATE TRIGGER roll_no BEFORE INSERT ON `students` FOR EACH ROW
                  BEGIN
                  SET @roll_num = IFNULL((substring((SELECT student_roll_no FROM students WHERE class_code = NEW.class_code ORDER BY created_at DESC LIMIT 1),-2) + 1), `1`),
                  NEW.student_roll_no = CONCAT(YEAR(CURRENT_DATE)),
                  NEW.class_code,
                  IF (@roll_num < 10,
                  CONCAT(`0`, @roll_num),
                  @roll_num
                  )
                  END');


                  for example please check this link :



                  [https://itsolutionstuff.com/post/how-to-add-mysql-trigger-from-migrations-in-laravel-5example.html]1



                  i hope help you







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 6 at 13:14









                  MohammadMohammad

                  3651414




                  3651414




















                      HARIKRISHNA is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      HARIKRISHNA is a new contributor. Be nice, and check out our Code of Conduct.












                      HARIKRISHNA is a new contributor. Be nice, and check out our Code of Conduct.











                      HARIKRISHNA is a new contributor. Be nice, and check out our Code of Conduct.














                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023271%2fhow-to-use-triggers-in-laravel%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                      Алба-Юлія

                      Захаров Федір Захарович