Laravel: Generating Files All Files at Once with PHP Artisan Single Command for Web and API

Abu Sayed
4 min readMar 6, 2024

--

Discover the power of Laravel’s PHP Artisan command-line tool. Learn how to generate Model, Controller, Migration, Resource, and Seeder files for both web and API projects with a single command, saving time and streamlining your development process.

Learn Laravel — Generating Files with PHP Artisan Single Command for Web and API

Y’all, Laravel’s Artisan is like your personal genie when it comes to development. With a simple command, you can summon all sorts of files like Models, Controllers, Migrations, and more — it’s like having a magic wand for your code! 🪄

But wait, there’s more! Artisan doesn’t just work for your typical web app. Nope, it’s also got your back for building APIs. Imagine being able to generate files for both web and API projects with a single command? Talk about efficiency! 💨

The Setup

Before we dive in, let’s make sure we’re all on the same page. You’ll need to have Laravel installed and set up on your machine. If you’re new to this, don’t sweat it — Laravel’s got great docs to help you get started. 📚

Once you’ve got that sorted, open up your terminal and navigate to your project’s directory. It’s showtime, folks! 🎬

Generating Files for Web

Alright, let’s start with the web side of things. Say you want to create a new Model, Controller, Migration, and Resource for your blog posts. Easy peasy!

php artisan make:model Post -mcr
Generating Files for Web
php artisan make:model Post -mcr

Boom! 💥 With that one command, you just created:

  • A Post model in app/Models/Post.php
  • A PostController in app/Http/Controllers/PostController.php
  • A migration file for the posts table in database/migrations
  • A PostResource in app/Http/Resources/PostResource.php

Pretty nifty, right? But what if you need a seeder too? No problem!

php artisan make:model Post -mcrs
php artisan make:model Post -mcrs

That’ll generate a PostSeeder in database/seeders/PostSeeder.php for you as well. Easy as pie! 🥧

Generating Files for API

Now, let’s talk about APIs. Building a robust API is like constructing a solid foundation for your app — you gotta do it right from the start. 🧱

Let’s say you’re creating a new API resource for managing users. Here’s how you’d generate the necessary files:

php artisan make:model User — api
Generating Files for API
php artisan make:model User --api

This command creates:

  • A User model in app/Models/User.php
  • A UserController in app/Http/Controllers/Api/UserController.php (notice the Api directory!)
  • A migration file for the users table in database/migrations
  • A UserResource in app/Http/Resources/UserResource.php

Pretty cool, huh? But what if you need a seeder too? Easy!

php artisan make:model User --api -s

That’ll generate a UserSeeder in database/seeders/UserSeeder.php as well.

A Real-Life Example

Alright, let’s put this into action with a real-life example. Imagine you’re building an e-commerce app with products, categories, and orders.

First, let’s create the Product model, controller, migration, resource, and seeder:

php artisan make:model Product -mcrs

Next, let’s do the same for Category:

php artisan make:model Category -mcrs

And finally, let’s generate the files for Order, but this time for the API:

php artisan make:model Order --api -s

Boom! 💥 You’ve just created the foundation for your e-commerce app with a few simple commands. Isn’t Artisan just the best?

The Code Breakdown

Okay, so we’ve seen how to use Artisan to generate files, but what’s actually going on under the hood? Let’s take a peek!

Here’s a small program that demonstrates how Artisan generates a model, controller, migration, and resource:

<?php
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
class GenerateFilesCommand extends Command
{
protected $signature = 'generate:files {name}';
public function handle()
{
$name = $this->argument('name');
// Generate Model
Artisan::call('make:model', ['name' => $name]);
// Generate Controller
$controllerName = $name . 'Controller';
Artisan::call('make:controller', ['name' => $controllerName]);
// Generate Migration
$tableName = Str::snake(Str::pluralStudly($name));
Artisan::call('make:migration', ['name' => 'create_' . $tableName . '_table', '--create' => $tableName]);
// Generate Resource
$resourceName = $name . 'Resource';
Artisan::call('make:resource', ['name' => $resourceName]);
}
}

Let’s break this down:

  1. We define a new command with the signature generate:files {name}. This command takes the name of the model as an argument.
  2. In the handle method, we first get the name argument.
  3. We call Artisan::call('make:model', ['name' => $name]) to generate the model.
  4. We generate the controller by constructing the controller name ($controllerName) and calling Artisan::call('make:controller', ['name' => $controllerName]).
  5. For the migration, we create a snake-cased, plural table name ($tableName) and call Artisan::call('make:migration', ['name' => 'create_' . $tableName . '_table', '--create' => $tableName]).
  6. Finally, we generate the resource by constructing the resource name ($resourceName) and calling Artisan::call('make:resource', ['name' => $resourceName]).

This simple program demonstrates how you can use Artisan to automate the generation of various files for your Laravel application. Pretty cool, right?

Artisan, Your Coding Sidekick

There you have it, folks! Laravel’s Artisan is like your trusty coding sidekick, making file generation a breeze for both web and API projects. Whether you’re creating a new model, controller, migration, resource, or seeder, Artisan has your back.

So, the next time you’re starting a new project or adding a new feature, don’t hesitate to let Artisan do the heavy lifting. With its simple commands and versatility, you’ll be able to kick off your development process with ease and efficiency. 🚀

Happy coding, my friends! And remember, with Artisan by your side, you’re unstoppable! 💪

--

--

Abu Sayed

Bangladeshi Full Stack Web Dev, Sys Admin & DevOps Engineer. Skills: Data analysis, SQL, Kubernetes. Python, PHP & Laravel. Me on: bd.linkedin.com/in/imabusayed