Auto save subpages to database

Can someone show me how to autosave pages to database.

if i create a page, it will save to db and so with deletion.

i understand this https://getkirby.com/docs/developer-guide/toolkit/database

but im clueless in how to code the connection with panel and db

i searched here but cnt seems to find what im looing for

You can use hooks to hook in to Panel events. From there, you can then update your database.

i tried this

kirby()->hook('panel.page.create', function($page) {
  if($page->template() == 'course') {
    if($courses = db::table('courses')->insert(array(
      'uri' => $page->uid(),
      'title'    => $page->title(),
      'status' => 1
    )));
  }
  elseif ($page->template() == 'module') {
    if($courses = db::table('modules')->insert(array(
      'title' => $page->title(),
      'uri' => $page->uid()
    )));
  }
});

can I ask what is the right format for page template. I mean the template when you create a page.
what im using right now is not right.

and also how to get the page ID when page is created.

thank you

I’d actually use intendedTemplate() instead of template() here. $page->uid() gives you the untranslated ID of the page, so that is correct. But I think you have to cast to string everywhere:

if($page->template() == 'course') {
    if($courses = db::table('courses')->insert(array(
      'uri' => $page->uid()->value(),
      'title'    => $page->title()->value(),
      'status' => 1
    )));
  }

intendedTemplate() save my day. thanks @texnixe