njdubois Posted March 13, 2017 Share Posted March 13, 2017 Hi! 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..."); $confObj->save(); } 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! Thanks, Nick Quote Link to comment https://forums.phpfreaks.com/topic/303448-call-function-when-ever-eloquent-model-has-crud-operation/ Share on other sites More sharing options...
njdubois Posted March 13, 2017 Author Share Posted March 13, 2017 I think i may have accidentally stumbled on a clue. https://laracasts.com/discuss/channels/eloquent/listen-to-any-saveupdatecreate-event-for-any-model Quote Link to comment https://forums.phpfreaks.com/topic/303448-call-function-when-ever-eloquent-model-has-crud-operation/#findComment-1544198 Share on other sites More sharing options...
njdubois Posted March 13, 2017 Author Share Posted March 13, 2017 Oh my gosh!! I'm so sorry. This is a cakePhp project. Not a laravel project. Wishful thinking... Quote Link to comment https://forums.phpfreaks.com/topic/303448-call-function-when-ever-eloquent-model-has-crud-operation/#findComment-1544201 Share on other sites More sharing options...
njdubois Posted March 13, 2017 Author Share Posted March 13, 2017 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! Quote Link to comment https://forums.phpfreaks.com/topic/303448-call-function-when-ever-eloquent-model-has-crud-operation/#findComment-1544203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.