Detect if page is used in panel or frontend

I’m using the panel as backend for an existing legacy database. See Creating pages from database.

I need to show every record in the panel but filter the records for the frontend (in parallel at the same time while the editor is logged into the panel and also has a frontend tab open).

The only way I managed to do this is by checking the URL for starting with “/api/pages” or not. Is there a more elegant way to do this?

class SomeTablePage extends Page {
  public function children() {
    if (... we are in the panel ...) {
      $rows = Db::table('...')->select([ ... ])->all();
   } else {
      $rows = Db::table('...')->select([ ... ])->where('published = 1 AND ... other conditions ...')->all();