Jump to content
Sign in to follow this  

Call function when ever eloquent model has CRUD operation.

Recommended Posts

I'd like to create a function in an eloquent model that gets called whenever create() or save() is called on that model. This function updates an "updated_at" time stamp in a parent table.  
I don't even know what to search for in google that will lead me to a tutorial, or documentation explaining this.
For example:
I have 2 tables, Conferences (has "updated_at" timestamp field) and Maps (When ever a new map is saved, or existing updated I want to update the timestamp field "updated_at" in the Conferences table).
I know I could create a class with a function that does this and do a find for ->save() and after call my function.  
But why do that?  Shouldn't I be able to put a method in the Maps Model that, when CRUD happens, will automatically fire?
I guess it would look something like this:
    private function onCrud() {
        $confObj = Conference::where("id", "=", $session(['currentConfId']))->first();
        $confObj->updated_at = date("whatever format...");

How do I tell laravel/eloquent to fire this method when ever a save is called on the model?


Any direction with this is greatly appreciated!

Share this post

Link to post
Share on other sites

Oh my gosh!! I'm so sorry.  This is a cakePhp project.  Not a laravel project.  Wishful thinking...

Share this post

Link to post
Share on other sites

Ok, I apologize a million times.  I'd delete this post completely if I could figure out how.


I am normally a laravel developer and most of our projects are laravel, except this one which is cakephp.


Regardless, I realize now that when I need to look into is event listeners.  I should be able to find more targeted documentation on this and maybe solve my own problem.


Thanks anyways!

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.