laravel 现有表 生成 ORM模型

laravel 现有表 生成 ORM 模型

use Illuminate\Support\Facades\Schema;

  public function test()
    {
        
        $tables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
        foreach ($tables as $table) {
            $columns = Schema::getColumnListing($table);

            $columns = implode("','",$columns);

            $columns = "['$columns']";


            $class = $table;
            $tables = $table;
            $class = explode('_', $class);
            $className = '';
            if (count($class) == 1) {
                $className = $class[0];
            } else {
                for ($i = 1; $i < count($class); $i++) {
                    $className .= ucwords($class[$i]);
                }
            }

            $path = 'Models\\' . ucwords($class[0]);

            $path = app_path($path) . "\\";
            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            } else {
                chmod($path, 0777);
            }

            $file = $className . '.php';

            if (file_exists($path . $file)) {
                echo $tables;
                continue;
            }

            $r = fopen($path . $file, 'w');

            $dir = ucwords($class[0]);
            $text = "

你可能感兴趣的:(laravel,php)