Laravel Artisan & Composer Commands Cheatsheet
This Cheatsheet will show you the most commonly used Laravel Artisan commands with their description.
Install Laravel globally
This command will install laravel globally in your system, further, if you want to create laravel projects just run
laravel new your-project-name
composer global require laravel/installer
make:request
This command will create a new form request file inside app/Http/Requests/
php artisan make:request RequestName
tinker
This command will open an interactive shell for testing and debugging mostly used for creating and inserting fake data to the database.
php artisan tinker
Ignore other dependencies while installing any Laravel package
To ignore other dependencies while installing any Laravel package using Composer, you can use the
--ignore-platform-reqs option with the composer require command. This option tells Composer to ignore the PHP version and extensions requirements for the package and its dependencies.
//This command will install the package and ignore the platform requirements
composer require package-name --ignore-platform-reqs
//This command will install the package and skip running any post-install scripts
composer require package-name --no-scripts
// This command will install the package without updating the existing dependencies
composer require package-name --no-update
optimize
This command will optimize your application for faster performance.
php artisan optimize
config:cache
This command will cache the configuration files for faster performance.
php artisan config:cache
storage:link
This command will create a symbolic link from "public/storage" to "storage/app/public"
php artisan storage:link
Laravel Clear Cache
- Clear Route Cache in Laravel: php artisan route:cache
- Clear Application Cache in Laravel: php artisan cache:clear
- Clear Config Cache in Laravel: php artisan config:cache
- Clear View Cache in Laravel: php artisan view:clear
//Clear Route Cache
php artisan route:cache
//Clear Application Cache
php artisan cache:clear
//Clear Config Cache
php artisan config:cache
//Clear View Cache
php artisan view:clear
cache:forget
cache:forget is used to remove an item from the cache The cache:forget command accepts a key argument, which is the key of the item to be removed from the cache. For example, to remove a cached item with the key users, you can run the following command.
php artisan cache:forget users
queue:forget
queue:forget is used to delete a failed job from the queue. When a job fails in Laravel's queue system, it is placed in the failed_jobs database table. The queue:forget command lets you delete a specific failed job from this table. It uses the ID of the failed job as the argument.
delete a failed job with an ID of 5
php artisan queue:forget 5
Display the current framework environment
The "php artisan env" command is used in the Laravel framework to display the current environment that the application is running in.
For example, if your ".env" file has the following line:
APP_ENV=local
Running the "php artisan env" command will display output as:
Current application environment: local
php artisan env
migrate:fresh
This command will drop all tables and re-run all migrations.
php artisan migrate:fresh
help
The "php artisan help" command is used to display the list of available Artisan commands or to get help on a specific Artisan command.
//display a list of available Artisan commands
php artisan help
//to get help on the "make:controller" command
php artisan help make:controller
//to get help on the "make:controller" command in json format
php artisan help make:controller --formate=json
//to get help on the "make:model" command
php artisan help make:model
//to get help on the "make:model" command in json format
php artisan help make:model --format=json
Start local development server
This command will start a development server on your local machine, which you can access by visiting http://localhost:8000 or http://127.0.0.1:8000 in your web browser.
php artisan serve
or
php artisan serve --port=8000
Run the application tests
The "php artisan test" command is used in the Laravel framework to run tests defined in your application. This command uses PHPUnit to execute your tests.
Here are some of the options that you can use with the "php artisan test" command (These are optional):
- "--without-tty": disables TTY mode, which is useful when running the tests in a non-interactive environment.
- "--compact": displays test results in a compact format, without any additional information.
- "--coverage": generates a code coverage report for your tests.
- "--min [MIN]": runs only the tests that took longer than the specified duration. The duration is specified in seconds.
- "-p|--parallel": runs tests in parallel, using multiple processes.
- "--profile": generates a performance profile of your tests, which can be used to identify slow tests.
- "--recreate-databases": recreates the test databases before running the tests.
- "--drop-databases": drops the test databases after running the tests.
- "--without-databases": runs the tests without using a database.
Note that the options and their behavior may vary depending on the version of Laravel and PHPUnit that you are using.
// to run the tests without options:
php artisan test
//to run the tests in parallel using 4 processes, you can run:
php artisan test --parallel -p 4
//to generate a code coverage report for your tests, you can run:
php artisan test --coverage
clear-compiled
php artisan clear-compiled is an Artisan command in Laravel that clears the compiled class file located in the bootstrap/cache directory
php artisan clear-compiled
make:auth
This command will generate views and controllers for user authentication.
php artisan make:auth
Laravel Package Installation Command
composer require packagename
//or install any package by ignoring palteform dependency
composer require packagename--ignore - platform - reqs
//Laravel/UI (for scaffolding frontend)
composer require laravel / ui
//Laravel Excel: A package used to import and export Excel and CSV files in Laravel.
composer require maatwebsite / excel
//Laravel Collective (for HTML and form builder)
composer require laravelcollective / html
//Intervention Image (for image manipulation)
composer require intervention / image
//Spatie Permissions (for roles and permissions)
composer require spatie / laravel - permission
//Maatwebsite Excel (for Excel import and export)
composer require maatwebsite / excel
//Laravel Socialite (for social media authentication)
composer require laravel / socialite
//Laravel Breeze (for authentication)
composer require laravel / breeze--dev
//Laravel Jetstream (for authentication and scaffolding frontend)
composer require laravel / jetstream
//Laravel Sanctum (for API authentication)
composer require laravel / sanctum
//Laravel Debugbar: Used for debugging and profiling the Laravel application.
composer require barryvdh / laravel - debugbar
//Debugbar (for debugging and profiling)
composer require barryvdh / laravel - debugbar--dev
//Laravel Telescope: A beautiful debug assistant for the Laravel application.
composer require laravel / telescope
//Laravel Horizon: A beautiful dashboard and management system for Laravel queues.
composer require laravel / horizon
//Laravel Scout: A driver-based search package for Laravel.
composer require laravel / scout
//Laravel Socialite: Used for authenticating with social networks like Facebook, Twitter, and Google.
composer require laravel / socialite
//Laravel Backpack: A collection of admin panels for Laravel.
composer require backpack / crud
//Laravel Voyager: A complete admin panel for Laravel applications.
composer require tcg / voyager
//Laravel Permissions: A package used for handling permissions and authorization in Laravel.
composer require spatie / laravel - permission
//Laravel Breeze: A minimal, lightweight authentication system for Laravel.
composer require laravel / breeze
vendor:publish
when you run the php artisan vendor:publish command without any additional arguments, it will list down all available publishing groups (usually tagged) from various packages that support publishing. Then you can select a specific tag or provider to publish the associated vendor.
php artisan vendor:publish command will give you suggestions like this
Which provider or tag's files would you like to publish?
[0 ] Publish files from all providers and tags listed below
[1 ] Provider: Spatie\Permission\PermissionServiceProvider
[2 ] Provider: Laravel\Tinker\TinkerServiceProvider
[3 ] Tag: laravel-mail
[4 ] Tag: laravel-notifications
...
php artisan vendor:publish
/OR publish specific tag vendor/assets run this command
php artisan vendor:publish --tag=specify-tag-name-that-you-want-t-publish
/OR publish specific providor vendor/assets run this command
php artisan vendor:publish --provider="ProviderClassName"
vendor:publish Laravel default error page
php artisan vendor:publish --tag=laravel-errors
Artisan all in one command
This single command will generate 8 files
- a model file in the app/Models/ directory,
- a migration file in the database/migrations directory,
- a factory file in the database/factories directory,
- a controller file in the app/Http/Controllers directory.
- a seeder file in the database/seeders directory
- a store Request file in the app/Http/Requests/ directory.
- an update Request file in the app/Http/Requests/ directory.
- a Policy file in the app/Policies/ directory.
php artisan make:model Post --all
OR
php artisan make:model Post --all --seed
make:model
This command will create a new Eloquent model inside app/Http/Models
php artisan make:model ModelName
make:middleware
This command will create a new middleware inside app/Http/Middleware.
php artisan make:middleware MiddlewareName
Create Laravel project
This command will download and install all the required dependencies for your laravel projects.
You can also run the command with --prefer-dist to force downloading the latest version of laravel
composer create-project laravel/laravel your-project-name
OR
composer create-project --prefer-dist laravel/laravel your-project-name
make:controller
This command will create a new controller inside app/Http/Controller
php artisan make:controller ControllerName
make:migration
This command will create a new database migration file inside database/migrations/
php artisan make:migration create_table_name
route:list
This command will show you the list of all registered routes in the application.
php artisan route:list
migrate
This command will run all pending database migrations and generate table in database.
php artisan migrate
migrate:rollback
This command will rollback the last batch of migrations.
php artisan migrate:rollback
make:seed
This command will create a new create database seeder inside app/datase/seeders/
php artisan make:seed SeedName
db:seed
This command will run all database seeders and will insert the predefine data into database.
php artisan db:seed
auth:clear-resets
auth:clear-resets is an Artisan command in Laravel that clears expired password reset tokens from the password_resets table in the database.
php artisan auth:clear-resets