{"id":286,"date":"2023-01-21T20:26:44","date_gmt":"2023-01-21T17:26:44","guid":{"rendered":"https:\/\/akaunting.com\/hc\/?post_type=docs&#038;p=286"},"modified":"2023-01-21T20:26:44","modified_gmt":"2023-01-21T17:26:44","slug":"hooking-models","status":"publish","type":"docs","link":"https:\/\/akaunting.com\/hc\/docs\/developers\/hooking-models\/","title":{"rendered":"Hooking Models"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Let\u2019s say you\u2019re developing a module\/app that adds functionality to Akaunting. However, to keep Akaunting up-to-date, you don\u2019t want to, and shouldn\u2019t, change\/hack the core files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent fires\u00a0<a href=\"https:\/\/laravel.com\/docs\/eloquent#events\" target=\"_blank\" rel=\"noreferrer noopener\">several events<\/a>, allowing you to hook into many points in a model\u2019s lifecycle. You can listen to these events using\u00a0the\u00a0<a href=\"https:\/\/laravel.com\/docs\/eloquent#observers\" target=\"_blank\" rel=\"noreferrer noopener\">Observers<\/a>\u00a0feature of Eloquent. Let\u2019s make an example for modules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, create and install a\u00a0<a href=\"https:\/\/akaunting.com\/hc\/docs\/developers\/modules\/\" target=\"_blank\" rel=\"noreferrer noopener\">module<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the following code into the&nbsp;<code>boot<\/code>&nbsp;method of your&nbsp;<code>Service Provider<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">use App\\Models\\Common\\Item;\n\n...\n\n    public function boot()\n    {\n        Item::observe('Modules\\MyBlog\\Observers\\Common\\Item');\n    }\n\n...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then create the observer file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\"><strong>&lt;?php<\/strong>\n\nnamespace Modules\\MyBlog\\Observers\\Common;\n\nuse App\\Abstracts\\Observer;\nuse App\\Models\\Common\\Item;\nuse Modules\\MyBlog\\Models\\Post;\n\nclass Item extends Observer\n{\n    public function updated(Item $item): void\n    {\n        $post = Post::where('item_id', $item->id)->first();\n\n        $post->opening_stock = $item->quantity;\n\n        $post->update();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all you need to do to hook a model.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say you\u2019re developing a module\/app that adds functionality to Akaunting. However, to keep Akaunting up-to-date, you don\u2019t want to, and shouldn\u2019t, change\/hack the core files. Eloquent fires\u00a0several events, allowing you to hook into many points in a model\u2019s lifecycle. You can listen to these events using\u00a0the\u00a0Observers\u00a0feature of Eloquent. Let\u2019s make an example for modules. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":31,"menu_order":10,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-286","docs","type-docs","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":1,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/286\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/286\/revisions\/287"}],"up":[{"embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/docs\/31"}],"wp:attachment":[{"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/akaunting.com\/hc\/wp-json\/wp\/v2\/doc_tag?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}