How to use the command `php artisan` (with examples)
The php artisan
command is Laravel’s Artisan command-line interface. It provides a convenient way to interact with your Laravel application from the command line. With this command, you can perform various tasks like starting a web server, running interactive PHP commands, generating code, and more.
Use case 1: Start PHP’s built-in web server for the current Laravel application
Code:
php artisan serve
Motivation:
When developing a Laravel application, you often need to test it in a local web server. The php artisan serve
command allows you to quickly start PHP’s built-in web server for the current Laravel application. This saves you from configuring a separate web server like Apache or Nginx.
Explanation:
php artisan serve
: This command starts PHP’s built-in web server and serves your Laravel application on the default port 8000. You can access your application in a web browser athttp://localhost:8000
.
Example output:
Laravel development server started: http://127.0.0.1:8000
Use case 2: Start an interactive PHP command-line interface
Code:
php artisan tinker
Motivation:
Sometimes, you may need to quickly test and experiment with PHP code without going through the hassle of creating a separate PHP file. The php artisan tinker
command launches an interactive PHP command-line interface (REPL) tailored for Laravel, where you can execute PHP statements and interact with your application’s code.
Explanation:
php artisan tinker
: This command starts an interactive PHP REPL (Read-Eval-Print Loop) session for your Laravel application. It loads your application’s environment and provides access to your application’s code and libraries. You can execute arbitrary PHP statements, interact with your models, perform database queries, and more.
Example output:
Psy Shell v0.10.8 (PHP 7.4.16 — cli) by Justin Hileman
>>> $user = App\Models\User::first();
>>> $user->name;
=> "John Doe"
Use case 3: Generate a new Eloquent model class with a migration, factory, and resource controller
Code:
php artisan make:model ModelName --all
Motivation:
When developing a Laravel application, you often need to create new data models along with their associated database migrations, factory classes, and resource controllers. The php artisan make:model
command with the --all
option provides a convenient way to generate all these files with a single command.
Explanation:
php artisan make:model ModelName --all
: This command generates a new Eloquent model class namedModelName
, along with its associated migration, factory, and resource controller files. The--all
option tells Laravel to generate all the necessary files. These files provide a solid foundation for managing your application’s data, database schema, and HTTP resources.
Example output:
Model created successfully.
Created Migration: 2022_01_01_000000_create_model_names_table
Created Factory: ModelNameFactory
Controller created successfully.
Use case 4: Display a list of all available commands
Code:
php artisan help
Motivation:
Laravel’s Artisan command-line interface offers a wide range of commands for various development tasks. Sometimes, you may need a quick reference to see all the available commands and their descriptions. The php artisan help
command provides an overview of all the available commands in Laravel.
Explanation:
php artisan help
: This command displays a list of all available commands in Laravel’s Artisan command-line interface. It shows the command names, their descriptions, and any additional options or arguments they support. This is useful when you want to explore the available commands or refresh your memory about a specific command.
Example output:
Laravel Framework X.X.X
Usage:
command-name [options] [arguments]
Options:
...
...
...
Available commands:
command1 Description of command1
command2 Description of command2
...
...
Conclusion:
The php artisan
command is a powerful tool that provides a convenient way to manage various aspects of your Laravel application from the command line. It allows you to start a web server, run an interactive PHP REPL, generate code files, and explore all available commands. By leveraging the features of Laravel’s Artisan command-line interface, you can streamline your development workflow and save time.