Artisan is a command-line interface (CLI) included with the Laravel PHP framework that provides several helpful commands for speeding up development workflows. This tutorial will cover the basics of using Artisan and explain how it can help streamline the process of application development.
To use Artisan, you will need to install the Laravel framework first. Laravel uses Composer's PHP dependency manager to install and manage project dependencies.
To install Laravel, you must have PHP Composer installed on your machine. If you don't already have it, you can install it by running the following command:
curl -sS https://getcomposer.org/installer | php
Once Composer is installed, navigate to the directory where you want to create your Laravel project and run the following command:
composer create-project --prefer-dist laravel/laravel myproject
The above command will create a new Laravel project in a directory called "myproject".
To use Artisan, you need to open a terminal window and navigate to the root directory of your Laravel project. From here, you can run Artisan commands by typing
php artisan followed by the command you want to run.
For example, to see a list of all available Artisan commands, you can use the following command:
php artisan list
The above command will display a list of all available Artisan commands, along with a brief description of each.
One of the critical features of Artisan is its ability to configure the Laravel application using various commands. For example, Artisan can set the application's environment variables, which are used to store the application's configuration settings.
To set an environment variable, the
php artisan env:set command can be used, followed by the variable's name and its value. For example:
php artisan env:set APP_DEBUG true
The above command will set the
APP_DEBUG environment variable to
true, which will enable debug mode for the application.
One of the most useful features of Artisan is its ability to generate code. Laravel includes several generators that can generate skeletal code for different application parts, such as controllers, models, and migrations.
For example, to create a new controller named
make:controller command can be used:
php artisan make:controller BlogController
This will create a new file called BlogController.php in the app/Http/Controllers directory with the basic structure of a controller.
make:model command can also be used to create a new model:
php artisan make:model Blog
This will create a new file called Blog.php in the app directory with the basic structure of a model.
Laravel's database migration system allows developers to easily modify and share the application's database schema, such as creating new tables or adding columns to existing tables. These migrations are stored in the database/migrations directory. For example, the
make:migration command can be used to create a new migration:
php artisan make:migration tbl_blog
The above command will create a new file in the
database/migrations directory with a timestamp as part of the file name. The file will contain a basic migration class with a
Another example of creating a new migration is to add a title column to the tbl_blog table; The following commands can be used as follows:
php artisan make:migration add_title_column_to_tbl_blog_table --table=tbl_blog
To run the migrations, the
migrate command can be used as follows:
php artisan migrate
This above command runs all outstanding migrations, which will update the database schema to match the current state of the application.
Here are some additional commands that may be useful during application development:
php artisan make:auth: This command creates the necessary views and controllers for authentication.
php artisan make:seeder: This command creates a new database seeder class, which populates the database with test data.
php artisan optimize: This command optimizes various application parts for better performance, such as the class autoloader and route caching.
Laravel Artisan is a powerful tool that can save developers time and effort while building applications. Using the various commands it provides, it can quickly generate code, run migrations, and perform other tasks that would otherwise be tedious and time-consuming.