<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: macarthurgonde</title>
    <description>The latest articles on DEV Community by macarthurgonde (@macarthurgonde).</description>
    <link>https://dev.to/macarthurgonde</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1158869%2Fcb987caa-0b61-492f-bf9a-0e3c06b8027c.png</url>
      <title>DEV Community: macarthurgonde</title>
      <link>https://dev.to/macarthurgonde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/macarthurgonde"/>
    <language>en</language>
    <item>
      <title>Laravel Artisan command that automatically registers API routes based on your existing controllers.</title>
      <dc:creator>macarthurgonde</dc:creator>
      <pubDate>Fri, 05 Dec 2025 03:28:40 +0000</pubDate>
      <link>https://dev.to/macarthurgonde/laravel-artisan-command-that-automatically-registers-api-routes-based-on-your-existing-controllers-21p4</link>
      <guid>https://dev.to/macarthurgonde/laravel-artisan-command-that-automatically-registers-api-routes-based-on-your-existing-controllers-21p4</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Create the Artisan Command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:command GenerateApiRoutes

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create app/Console/Commands/GenerateApiRoutes.php.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implement the Command&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Replace the content with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class GenerateApiRoutes extends Command
{
    protected $signature = 'generate:api-routes';
    protected $description = 'Automatically create API endpoints based on Controllers';

    public function handle()
    {
        $controllerPath = app_path('Http/Controllers');
        $files = File::files($controllerPath);

        $routes = "";

        foreach ($files as $file) {
            $filename = $file-&amp;gt;getFilenameWithoutExtension();
            if (Str::endsWith($filename, 'Controller')) {
                $resource = Str::kebab(str_replace('Controller', '', $filename));
                $routes .= "Route::apiResource('{$resource}', App\\Http\\Controllers\\{$filename}::class);\n";
            }
        }

        $apiRoutesFile = base_path('routes/api_generated.php');

        File::put($apiRoutesFile, "&amp;lt;?php\n\nuse Illuminate\Support\Facades\Route;\n\n" . $routes);

        $this-&amp;gt;info("API routes generated in routes/api_generated.php");

        // Include this file in routes/api.php if not already included
        $apiFile = base_path('routes/api.php');
        $content = File::get($apiFile);
        if (!Str::contains($content, "api_generated.php")) {
            File::append($apiFile, "\nrequire __DIR__.'/api_generated.php';\n");
            $this-&amp;gt;info("api_generated.php included in routes/api.php");
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;How it Works&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The command scans app/Http/Controllers for all controllers ending with Controller.&lt;/p&gt;

&lt;p&gt;It creates an API resource route for each controller, following Laravel conventions.&lt;/p&gt;

&lt;p&gt;Example: CustomerController → /api/customers&lt;/p&gt;

&lt;p&gt;Writes all routes to routes/api_generated.php (so you don’t overwrite api.php).&lt;/p&gt;

&lt;p&gt;Automatically includes api_generated.php in routes/api.php if not already included.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Usage&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After creating your controllers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan generate:api-routes

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your API endpoints are ready and live. You can check routes/api_generated.php:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::apiResource('customers', App\Http\Controllers\CustomerController::class);
Route::apiResource('orders', App\Http\Controllers\OrderController::class);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>automation</category>
      <category>api</category>
      <category>php</category>
    </item>
    <item>
      <title>Creating Laravel Artisan Command To Generate Model Repository Service Controller for each table automatically.</title>
      <dc:creator>macarthurgonde</dc:creator>
      <pubDate>Fri, 05 Dec 2025 03:15:08 +0000</pubDate>
      <link>https://dev.to/macarthurgonde/creating-laravel-artisan-command-to-generate-model-repository-service-controller-for-each-3g55</link>
      <guid>https://dev.to/macarthurgonde/creating-laravel-artisan-command-to-generate-model-repository-service-controller-for-each-3g55</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Custom Artisan Command: Auto Generate Layers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:command GenerateCrudFromDb

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Implement the Command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Replace the file app/Console/Commands/GenerateCrudFromDb.php with:
&amp;lt;?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class GenerateCrudFromDb extends Command
{
    protected $signature = 'generate:crud {--table=*}';
    protected $description = 'Automatically generate Model, Repository, Service, Controller for tables';

    public function handle()
    {
        $tables = $this-&amp;gt;option('table');

        if (empty($tables)) {
            // If no table specified, get all tables
            $tables = $this-&amp;gt;getAllTables();
        }

        foreach ($tables as $table) {
            $this-&amp;gt;generateForTable($table);
        }

        $this-&amp;gt;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-&amp;gt;{'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-&amp;gt;getTableColumns($table);

        $this-&amp;gt;createModel($modelName, $columns);
        $this-&amp;gt;createRepository($repositoryName, $modelName);
        $this-&amp;gt;createService($serviceName, $repositoryName);
        $this-&amp;gt;createController($controllerName, $serviceName);

        $this-&amp;gt;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-&amp;gt;Field, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
                $fillable[] = $column-&amp;gt;Field;
            }
        }
        return $fillable;
    }

    protected function createModel($name, $fillable)
    {
        $modelPath = app_path("Models/{$name}.php");
        if (!File::exists($modelPath)) {
            $fillableArray = implode("','", $fillable);
            $content = "&amp;lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class {$name} extends Model
{
    use HasFactory;

    protected \$guarded = [];
    protected \$fillable = ['{$fillableArray}'];
}
";
            File::put($modelPath, $content);
            $this-&amp;gt;info("Model created: {$name}");
        }
    }

    protected function createRepository($name, $model)
    {
        $repoPath = app_path("Repositories/{$name}.php");
        if (!File::exists($repoPath)) {
            $content = "&amp;lt;?php

namespace App\Repositories;

use App\Models\\{$model};

class {$name}
{
    protected \${$model};

    public function __construct({$model} \${$model})
    {
        \$this-&amp;gt;{$model} = \${$model};
    }

    public function all() { return \$this-&amp;gt;{$model}::all(); }
    public function find(\$id) { return \$this-&amp;gt;{$model}::find(\$id); }
    public function create(array \$data) { return \$this-&amp;gt;{$model}::create(\$data); }
    public function update(\$id, array \$data) { 
        \$record = \$this-&amp;gt;find(\$id); 
        if(\$record) \$record-&amp;gt;update(\$data); 
        return \$record; 
    }
    public function delete(\$id) { 
        \$record = \$this-&amp;gt;find(\$id); 
        if(\$record) \$record-&amp;gt;delete(); 
        return \$record; 
    }
}
";
            File::put($repoPath, $content);
            $this-&amp;gt;info("Repository created: {$name}");
        }
    }

    protected function createService($name, $repository)
    {
        $servicePath = app_path("Services/{$name}.php");
        if (!File::exists($servicePath)) {
            $content = "&amp;lt;?php

namespace App\Services;

use App\Repositories\\{$repository};

class {$name}
{
    protected \${$repository};

    public function __construct({$repository} \${$repository})
    {
        \$this-&amp;gt;{$repository} = \${$repository};
    }

    public function all() { return \$this-&amp;gt;{$repository}-&amp;gt;all(); }
    public function find(\$id) { return \$this-&amp;gt;{$repository}-&amp;gt;find(\$id); }
    public function create(array \$data) { return \$this-&amp;gt;{$repository}-&amp;gt;create(\$data); }
    public function update(\$id, array \$data) { return \$this-&amp;gt;{$repository}-&amp;gt;update(\$id, \$data); }
    public function delete(\$id) { return \$this-&amp;gt;{$repository}-&amp;gt;delete(\$id); }
}
";
            File::put($servicePath, $content);
            $this-&amp;gt;info("Service created: {$name}");
        }
    }

    protected function createController($name, $service)
    {
        $controllerPath = app_path("Http/Controllers/{$name}.php");
        if (!File::exists($controllerPath)) {
            $content = "&amp;lt;?php

namespace App\Http\Controllers;

use App\Services\\{$service};
use Illuminate\Http\Request;

class {$name} extends Controller
{
    protected \${$service};

    public function __construct({$service} \${$service})
    {
        \$this-&amp;gt;{$service} = \${$service};
    }

    public function index() { return \$this-&amp;gt;{$service}-&amp;gt;all(); }
    public function store(Request \$request) { return \$this-&amp;gt;{$service}-&amp;gt;create(\$request-&amp;gt;all()); }
    public function show(\$id) { return \$this-&amp;gt;{$service}-&amp;gt;find(\$id); }
    public function update(Request \$request, \$id) { return \$this-&amp;gt;{$service}-&amp;gt;update(\$id, \$request-&amp;gt;all()); }
    public function destroy(\$id) { return \$this-&amp;gt;{$service}-&amp;gt;delete(\$id); }
}
";
            File::put($controllerPath, $content);
            $this-&amp;gt;info("Controller created: {$name}");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Directory Setup&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Make sure these exist:&lt;/p&gt;

&lt;p&gt;app/Models&lt;br&gt;
app/Repositories&lt;br&gt;
app/Services&lt;br&gt;
app/Http/Controllers&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Usage&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Generate for all tables in the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan generate:crud

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate for specific tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan generate:crud --table=customers --table=orders

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will automatically create:&lt;/p&gt;

&lt;p&gt;Models with $fillable from table columns&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Repositories

Services

Controllers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All following the Repository-Service pattern.&lt;/p&gt;

</description>
      <category>cli</category>
      <category>laravel</category>
      <category>automation</category>
      <category>php</category>
    </item>
  </channel>
</rss>
