Что бы Вы поменяли в данной функции проверки?

Здравствуйте! Оцените пожалуйста функцию проверки на существование или принадлежность пользователю с определённым ID:
<?php

require_once('database.php');

use Illuminate\Database\Capsule\Manager as Capsule;
class Checkers {
    public function check ($userid,$id , $type,$what) {
        if ($type==='dnsrec') {$table = 'dns';}
        if ($type==='domain') {$table ='domains';}
        if ($type==='vhost') {$table = 'vhosts';}
        if ($type==='mailbox') {$table = 'mailboxes';}
        if ($type==='mailalias'){$table= 'mail_aliases';}
        if ($type==='database') {$table = 'databases';}
        if ($what==='belong') {
            $result = Capsule::table($table)
                ->where('userid',  $userid)
                ->where('unid' , $id)
                ->first();
        }
        if ($what==='exist') {
            $result = Capsule::table($table)
                ->where('unid' , $id)
                ->first();
        }

        if (var_dump($result)==NULL) {return true;}
        else {return false;}

    }

}
  • Вопрос задан
  • 96 просмотров
Пригласить эксперта
Ответы на вопрос 1
Austin_Powers
@Austin_Powers
Web developer (Symfony, Go, Vue.js)
Код не проверенный. Но я бы сделал что-то похожее.
require_once('database.php');

use Illuminate\Database\Capsule\Manager as Capsule;
class Checkers {
    protected static $tables = [
      'dnsrec' => 'dns', 
     .... etc.
   ];
  
    public function check ($userid,$id , $type,$what) {
        $query = Capsule::table(self::$tables[$type]) ->where('unid' , $id);
        
        if ($what==='belong') {
            $result = $query->where('userid',  $userid)->first();
        } else if ($what==='exist') {
            $result = $query->first();
        }

        if (is_empty($result)) {
          return true;
        }
        
        return false;
    }

}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы