Creating Laravel Artisan Command To Generate Model Repository Service Controller for each table automatically.
Source: Dev.to
Custom Artisan Command: Auto Generate Layers
Run:
php artisan make:command GenerateCrudFromDb
Implement the Command
Replace the file app/Console/Commands/GenerateCrudFromDb.php with:
option('table');
if (empty($tables)) {
// If no table specified, get all tables
$tables = $this->getAllTables();
}
foreach ($tables as $table) {
$this->generateForTable($table);
}
$this->info('All CRUD layers generated successfully!');
}
protected function getAllTables()
{
$database = DB::getDatabaseName();
$tables = DB::select("SHOW TABLES");
$tables = array_map(function ($table) use ($database) {
return $table->{'Tables_in_' . $database};
}, $tables);
return $tables;
}
protected function generateForTable($table)
{
$className = Str::studly(Str::singular($table));
$modelName = $className;
$controllerName = $className . 'Controller';
$repositoryName = $className . 'Repository';
$serviceName = $className . 'Service';
$columns = $this->getTableColumns($table);
$this->createModel($modelName, $columns);
$this->createRepository($repositoryName, $modelName);
$this->createService($serviceName, $repositoryName);
$this->createController($controllerName, $serviceName);
$this->info("CRUD generated for table: {$table}");
}
protected function getTableColumns($table)
{
$columns = DB::select("SHOW COLUMNS FROM {$table}");
$fillable = [];
foreach ($columns as $column) {
if (!in_array($column->Field, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
$fillable[] = $column->Field;
}
}
return $fillable;
}
protected function createModel($name, $fillable)
{
$modelPath = app_path("Models/{$name}.php");
if (!File::exists($modelPath)) {
$fillableArray = implode("','", $fillable);
$content = "info("Model created: {$name}");
}
}
protected function createRepository($name, $model)
{
$repoPath = app_path("Repositories/{$name}.php");
if (!File::exists($repoPath)) {
$content = "{$model} = \${$model};
}
public function all() { return \$this->{$model}::all(); }
public function find(\$id) { return \$this->{$model}::find(\$id); }
public function create(array \$data) { return \$this->{$model}::create(\$data); }
public function update(\$id, array \$data) {
\$record = \$this->find(\$id);
if(\$record) \$record->update(\$data);
return \$record;
}
public function delete(\$id) {
\$record = \$this->find(\$id);
if(\$record) \$record->delete();
return \$record;
}
}
";
File::put($repoPath, $content);
$this->info("Repository created: {$name}");
}
}
protected function createService($name, $repository)
{
$servicePath = app_path("Services/{$name}.php");
if (!File::exists($servicePath)) {
$content = "{$repository} = \${$repository};
}
public function all() { return \$this->{$repository}->all(); }
public function find(\$id) { return \$this->{$repository}->find(\$id); }
public function create(array \$data) { return \$this->{$repository}->create(\$data); }
public function update(\$id, array \$data) { return \$this->{$repository}->update(\$id, \$data); }
public function delete(\$id) { return \$this->{$repository}->delete(\$id); }
}
";
File::put($servicePath, $content);
$this->info("Service created: {$name}");
}
}
protected function createController($name, $service)
{
$controllerPath = app_path("Http/Controllers/{$name}.php");
if (!File::exists($controllerPath)) {
$content = "{$service} = \${$service};
}
public function index() { return \$this->{$service}->all(); }
public function store(Request \$request) { return \$this->{$service}->create(\$request->all()); }
public function show(\$id) { return \$this->{$service}->find(\$id); }
public function update(Request \$request, \$id) { return \$this->{$service}->update(\$id, \$request->all()); }
public function destroy(\$id) { return \$this->{$service}->delete(\$id); }
}
";
File::put($controllerPath, $content);
$this->info("Controller created: {$name}");
}
}
}
Directory Setup
Ensure the following directories exist in your Laravel project:
app/Modelsapp/Repositoriesapp/Servicesapp/Http/Controllers
Usage
Generate for all tables in the database
php artisan generate:crud
Generate for specific tables
php artisan generate:crud --table=customers --table=orders
The command will automatically create:
- Models with
$fillablepopulated from table columns - Repositories implementing basic CRUD operations
- Services that delegate to the repositories
- Controllers exposing standard RESTful actions
All layers follow the Repository‑Service pattern.