Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions src/Commands/MigrationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,58 @@ protected function makeModel()
$modelPath = $this->getModelPath($this->getModelName());

if ($this->option('model') && !$this->files->exists($modelPath)) {
$this->call('make:model', [
'name' => $this->getModelName()
]);
$this->files->put($modelPath, $this->compileModelStub());
}

}

/**
* Compile the model stub.
*
* @return string
*/
protected function compileModelStub()
{
$stub = $this->files->get(__DIR__ . '/../stubs/model.stub');

$this->replaceModelName($stub)
->replaceFillable($stub)
->replaceTableName($stub);

return $stub;
}

/**
* Replace the model name in the stub.
*
* @param string $stub
* @return $this
*/
protected function replaceModelName(&$stub)
{
$stub = str_replace('{{name}}', $this->getModelName(), $stub);

return $this;
}

/**
* Replace the model fillable in the stub.
*
* @param string $stub
* @return $this
*/
protected function replaceFillable(&$stub)
{
if ($schema = $this->option('schema')) {
$schema = (new SchemaParser)->parse($schema);
}
$padvalue = function($s){ return "'{$s['name']}'"; };
$paddedFields = implode(', ', array_map($padvalue, $schema));
$paddedFields = "'". substr($paddedFields, 2);

$stub = str_replace('{{fillable}}', $paddedFields, $stub);

return $this;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/stubs/model.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class {{name}} extends Model
{
protected $table = '{{table}}';

/**
* The properties of this model that can be filled automatically
*/
protected $fillable = [{{fillable}}];
}