Laracademy\Generators\Commands\ModelFromTableCommand::handle PHP Method

handle() public method

Execute the console command.
public handle ( ) : mixed
return mixed
    public function handle()
    {
        $this->doComment('Starting Model Generate Command', true);
        $this->getOptions();
        $tables = [];
        $path = $this->options['folder'];
        $modelStub = file_get_contents($this->getStub());
        // can we run?
        if (strlen($this->options['table']) <= 0 && $this->options['all'] == false) {
            $this->error('No --table specified or --all');
            return;
        }
        // figure out if we need to create a folder or not
        if ($this->options['folder'] != app_path()) {
            if (!is_dir($this->options['folder'])) {
                mkdir($this->options['folder']);
            }
        }
        // figure out if it is all tables
        if ($this->options['all']) {
            $tables = $this->getAllTables();
        } else {
            $tables = explode(',', $this->options['table']);
        }
        // cycle through each table
        foreach ($tables as $table) {
            // grab a fresh copy of our stub
            $stub = $modelStub;
            // generate the file name for the model based on the table name
            $filename = str_singular(ucfirst($table));
            $fullPath = "{$path}/{$filename}.php";
            $this->doComment("Generating file: {$filename}.php");
            // gather information on it
            $model = ['table' => $table, 'fillable' => $this->getSchema($table), 'guardable' => [], 'hidden' => [], 'casts' => []];
            // fix these up
            $columns = $this->describeTable($table);
            // use a collection
            $this->columns = collect();
            foreach ($columns as $col) {
                $this->columns->push(['field' => $col->Field, 'type' => $col->Type]);
            }
            // replace the class name
            $stub = $this->replaceClassName($stub, $table);
            // replace the fillable
            $stub = $this->replaceModuleInformation($stub, $model);
            // figure out the connection
            $stub = $this->replaceConnection($stub, $this->options['connection']);
            // writing stub out
            $this->doComment('Writing model: ' . $fullPath, true);
            file_put_contents($fullPath, $stub);
        }
        $this->info('Complete');
    }