Getting Started with Laravel Artisan CLI

There are many PHP frameworks to choose from! Laravel is one such framework, and it has been around for more than a decade. Laravel increased in popularity following the release of version three in February 2012. This is because of the usefulness of the Laravel Artisan Command Line Interface (CLI).

Artisan helps the Laravel app development process through a handy CLI tool. It will give you the ability to perform database and file operations all from one place.

You may also want to read: How to install Laravel and set up a project.

Prerequisites

Artisan requires only the Laravel project to be set up. Laravel 3+ comes with Artisan built-in.

Below are the requirements for the latest version of Laravel, installed via Composer.

  1. PHP version 7.3+
  2. Composer

This post assumes that you have installed the above prerequisites.

Installation

Laravel installation using Composer is quite simple. You can run the following command to install Laravel in your current directory:

composer create-project laravel/laravel my-laravel-app

Note that the name of the Laravel project is “my-laravel-app”. You can of course change it according to your liking.

Getting Started

To start your development process, navigate to the Laravel project root directory.

cd my-laravel-app

Run the following command to make sure Laravel and Artisan are configured:

php artisan -v

As a result, the command mentioned above should output the Laravel version number.

Next, you may want to start your web server. You can do this by running the following command:

php artisan serve

As a result, the command will start a web server and output the server details. If you’re using the default settings, you can visit: “http://127.0.0.1:8000” to see your app running.

Artisan Commands

In the directory, you will notice a PHP executable file named “artisan”. To list all commands available using Artisan, run:

php artisan list

To get help for any of the above-listed commands, use:

php artisan help <command>

Make Command

Throughout your development process, you will need to create many files. Consequently, the make command allows the user to set up required files for a Laravel operation. The three most common parts of any Laravel app are Models, Views, and Controllers.

The basic syntax for the make command is:

make:controller [options] <name>

To create a model, you can run:

php artisan make:model myModel

In case you need a reminder, Models are object representations of database tables. As per the above-mentioned example, “myModel” is the name of the model. Of course, be aware that models are stored in the app/ folder. In some cases, it might also be stored in the app/model/ folder. Additionally, there are a few options that come with the make:model command.

For instance, you can add the –migration or -m option to make the migration file along with the model. In case you need a reminder, a Migration is a database modification activity in Laravel. Thus allowing the user to change the database directly from the Artisan CLI. Migrations are helpful because they allow Laravel apps to be self-sufficient.

Some other options for the make:model command are:

 -a, --all             Generate a migration, seeder, factory, and resource controller for the model
  -c, --controller      Create a new controller for the model
  -f, --factory         Create a new factory for the model
      --force           Create the class even if the model already exists
  -m, --migration       Create a new migration file for the model
  -s, --seed            Create a new seeder file for the model
  -p, --pivot           Indicates if the generated model should be a custom intermediate table model
  -r, --resource        Indicates if the generated controller should be a resource controller
      --api             Indicates if the generated controller should be an API controller
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

You can make controllers for Laravel using:

php artisan make:controller myController

Migrate Command

Once you have made your first model and configured the migration, you can run the migrations.

php artisan migrate

This will execute all the pending migrations and make changes in the database. All the migrations are stored in /db/migrations/ folder. You can rollback incorrect migrations using:

php artisan migrate:rollback

You can also add the –pretend or -p option to see what the executed query will be without executing it. Below is a complete list of options for the migrate command:

      --database[=DATABASE]  The database connection to use
      --force                Force the operation to run when in production
      --path[=PATH]    The path(s) to the migrations files to be executed (multiple values allowed)
      --realpath           Indicate any provided migration file paths are pre-resolved absolute paths
      --pretend            Dump the SQL queries that would be run
      --seed                Indicates if the seed task should be re-run
      --step                  Force the migrations to be run so they can be rolled back individually
  -h, --help                Display this help message
  -q, --quiet               Do not output any message
  -V, --version           Display this application version
      --ansi                 Force ANSI output
      --no-ansi            Disable ANSI output
  -n, --no-interaction       Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug  

More Artisan Commands

Finally, this was only the beginning of the Artisan CLI. You should explore the complete list of Artisan commands using the help command.

Below is a list of all the commands in Artisan CLI:

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version       Display this application version
      --ansi             Force ANSI output
      --no-ansi        Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]        The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  clear-compiled  Remove the compiled class file
  down                 Put the application into maintenance mode
  env                   Display the current framework environment
  help                  Display help for a command
  inspire              Display an inspiring quote
  list                    List commands
  migrate            Run the database migrations
  optimize           Cache the framework bootstrap files
  preset               Swap the front-end scaffolding for the application
  serve                Serve the application on the PHP development server
  tinker                Interact with your application
  UI                     Swap the front-end scaffolding for the application
  up                     Bring the application out of maintenance mode
 auth
  auth:clear-resets    Flush expired password reset tokens
 cache
  cache:clear          Flush the application cache
  cache:forget         Remove an item from the cache
  cache:table          Create a migration for the cache database table
 config
  config:cache         Create a cache file for faster configuration loading
  config:clear         Remove the configuration cache file
 db
  db:seed              Seed the database with records
  db:wipe              Drop all tables, views, and types
 debugbar
  debugbar:clear       Clear the Debugbar Storage
 event
  event:cache          Discover and cache the application's events and listeners
  event:clear          Clear all cached events and listeners
  event:generate       Generate the missing events and listeners based on registration
  event:list           List the application's events and listeners
 key
  key:generate         Set the application key
 make
  make:channel         Create a new channel class
  make:command         Create a new Artisan command
  make:controller      Create a new controller class
  make:event           Create a new event class
  make:exception       Create a new custom exception class
  make:factory         Create a new model factory
  make:job             Create a new job class
  make:listener        Create a new event listener class
  make:mail            Create a new email class
  make:middleware      Create a new middleware class
  make:migration       Create a new migration file
  make:model           Create a new Eloquent model class
  make:notification    Create a new notification class
  make:observer        Create a new observer class
  make:policy          Create a new policy class
  make:provider        Create a new service provider class
  make:request         Create a new form request class
  make:resource        Create a new resource
  make:rule            Create a new validation rule
  make:seeder          Create a new seeder class
  make:test            Create a new test class
 migrate
  migrate:fresh        Drop all tables and re-run all migrations
  migrate:install      Create the migration repository
  migrate:refresh      Reset and re-run all migrations
  migrate:reset        Rollback all database migrations
  migrate:rollback     Rollback the last database migration
  migrate:status       Show the status of each migration
 notifications
  notifications:table  Create a migration for the notifications table
 optimize
  optimize:clear       Remove the cached bootstrap files
 package
  package:discover     Rebuild the cached package manifest
 queue
  queue:failed         List all of the failed queue jobs
  queue:failed-table   Create a migration for the failed queue jobs database table
  queue:flush          Flush all of the failed queue jobs
  queue:forget         Delete a failed queue job
  queue:listen         Listen to a given queue
  queue:restart        Restart queue worker daemons after their current job
  queue:retry          Retry a failed queue job
  queue:table          Create a migration for the queue jobs database table
  queue:work           Start processing jobs on the queue as a daemon
 route
  route:cache          Create a route cache file for faster route registration
  route:clear          Remove the route cache file
  route:list           List all registered routes
 schedule
  schedule:run         Run the scheduled commands
 session
  session:table        Create a migration for the session database table
 storage
  storage:link         Create a symbolic link from "public/storage" to "storage/app/public"
 ui
  ui:auth              Scaffold basic login and registration views and routes
 vendor
  vendor:publish       Publish any publishable assets from vendor packages
 video
  video:add            Move video file and add new record to DB
  video:recognition    Recognition video by path to file
 view
  view:cache           Compile all of the application's Blade templates
  view:clear           Clear all compiled view files

Want to know more?

There is to much more that you need to know about Laravel.

Source:



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

Your email address will not be published. Required fields are marked *