diff --git a/src/Commands/Database/Eloquent.php b/src/Commands/Database/Eloquent.php index 02d7dbcc..af44814d 100644 --- a/src/Commands/Database/Eloquent.php +++ b/src/Commands/Database/Eloquent.php @@ -64,7 +64,7 @@ public function codeHasBeenGenerated(string $className): int $this->createSeeder($className); } - if ($this->option('controller') || $this->option('resource')) { + if ($this->option('controller') || $this->option('resource') || $this->option('api')) { $this->createController($className); } @@ -120,10 +120,11 @@ protected function createController(string $eloquentClassName): void { $controller = Str::studly(\class_basename($this->argument('name'))); - $this->call('make:controller', [ + $this->call('make:controller', \array_filter([ 'name' => "{$controller}Controller", - '--model' => $this->option('resource') ? $eloquentClassName : null, - ]); + '--model' => $this->option('resource') || $this->option('api') ? $eloquentClassName : null, + '--api' => $this->option('api'), + ])); } /** @@ -156,6 +157,7 @@ protected function getOptions() ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder file for the model'], ['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'], ['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'], + ['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an api controller'], ]; } } diff --git a/tests/Feature/Generators/Database/EloquentTest.php b/tests/Feature/Generators/Database/EloquentTest.php index a4a941ec..019e4afc 100644 --- a/tests/Feature/Generators/Database/EloquentTest.php +++ b/tests/Feature/Generators/Database/EloquentTest.php @@ -216,6 +216,34 @@ public function it_can_generate_nested_eloquent_with_resource_controller_options $this->assertFilenameNotExists('database/seeds/FooSeeder.php'); } + /** @test */ + public function it_can_generate_nested_eloquent_with_api_controller_options_file() + { + $this->artisan('make:model', ['name' => 'Foo/Bar', '--api' => true, '--no-interaction' => true]) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Foo;', + 'use Illuminate\Database\Eloquent\Model;', + 'class Bar extends Model', + ], 'app/Foo/Bar.php'); + + $this->assertFileContains([ + 'namespace App\Http\Controllers;', + 'use App\Foo\Bar;', + 'use Illuminate\Http\Request;', + 'class BarController extends Controller', + 'public function index()', + 'public function store(Request $request)', + 'public function show(Bar $bar)', + 'public function update(Request $request, Bar $bar)', + 'public function destroy(Bar $bar)', + ], 'app/Http/Controllers/BarController.php'); + + $this->assertFilenameNotExists('database/factories/FooFactory.php'); + $this->assertFilenameNotExists('database/seeds/FooSeeder.php'); + } + /** @test */ public function it_can_generate_eloquent_with_all_options_file() {