Laravel Artisan Commands List to Enhance Performance in Minutes

8 Minutes to read
Laravel Artisan is a useful command line tool that comes with the Laravel framework. It allows you to do many things like create a new Laravel project, run database migrations, clear the cache and more. Using Artisan can save developers hours of time compared to doing these things manually.

This article will provide a list of 12 helpful Artisan commands that make development in Laravel faster and easier. Whether you are just starting with Laravel or have been using it for a while, learning these commands can boost your productivity.

Artisan commands let you:

Knowing these commands will save you time when building Laravel apps. You won’t have to do tedious tasks manually anymore.
Laravel Artisan offers many handy commands. Learning 12 key ones can speed up development significantly. This article lists the most useful commands with examples.

1. Creating a New Laravel Project

The laravel new command creates a fresh Laravel project for you.

To use it, open your terminal and type:
javascript
laravel new my-app
Replace my-app with the name you want for your app.
This will create a new folder called my-app with a fresh Laravel install inside.

Benefits:

You don’t have to do any of that setup manually. The laravel new Artisan command handles it for you quickly.
This speeds up the process of starting new Laravel projects. You can focus on building the app instead of installing and configuring Laravel.

Key Takeaway: Use laravel new to instantly create new Laravel projects. It configures everything so you can start coding faster.

2. Generating Boilerplate Code

Laravel Artisan offers make: commands to create basic code structures. These commands save time by generating files with standard code.

Model Creation:

javascript
php artisan make:model Post
This creates a new model file named Post.php.

Controller Creation:

javascript
php artisan make:controller PostController

This generates a PostController.php file with basic methods.

Migration Creation:

javascript
php artisan make:migration create_posts_table
This makes a new migration file to set up a ‘posts’ table.

Benefits of using make: commands:

You can create models, controllers, and migrations faster with these commands. They set up the basic structure, so you only need to add your specific code.

Bonus Tip: You can combine commands. For example:

javascript
php artisan make:model Post -mc
This creates a model, migration, and controller all at once.
Using these Laravel Artisan commands helps you write less boilerplate code. You can focus more on the unique parts of your app.

3. Database Management

Laravel Artisan has handy commands for managing your database without hassle. This saves you time compared to doing database tasks manually.

Running Migrations:

javascript
php artisan migrate
This run all outstanding migration files to update the database schema. It creats new tables, columns etc base on your migration code.

Rolling Back Migrations:

javascript
php artisan migrate:rollback
Undo recent migration by reverting the changes. Useful for testing workflow.

Database Seeding:

javascript
php artisan db:seed
Seeders insert dummy data into tables. The db:seed command runs all seeder classes.

Benefits:

These Artisan command provide easy database management for Laravel projects. You can quick make schema changes and insert test data as needed.

4. Clearing Application Cache

Laravel caches data like views, routes and configurations for performance. Clearing the caches help optimize application speed.

Clear Route Cache:

php
php artisan route:cache
This command cache all route definitions for faster route registration.

Clear Configuration Cache:

javascript
php artisan config:cache
Caches config files for faster access. Use after updating configs.

Clear Application Cache:

javascript
php artisan cache:clear
Flush application cache stored in storage/framework/cache.

Benefits

These will provide a quick way to re-cache data for performance gains. Laravel app gets slow over time as cache expires. Periodically clearing and re-caching helps speed things up.

5. Managing Routes

Laravel Artisan provides handy commands for managing routes. This allows you to see and cache all routes for your app.

Show Route List:

javascript
php artisan route:list
Display all routes defined in app/Http/routes.php. Useful for inspecting routes.

Cache Routes:

javascript
php artisan route:cache
As mentioned previously, this caches all routes for faster registration. Should run after changing routes.

Remove Route Cache:

javascript
php artisan route:clear
Flush route cache and delete cached routes file.

Benefits:

These commands gives you visibility into your routes. You can view, cache, and clear as needed for debugging or performance.
Caching is important because registering all app routes on each request can slow down app. Caching avoids that by using cached route file instead.

6. Scheduling Tasks

Laravel Artisan lets you schedule tasks easy. You can set up regular jobs without messing with cron jobs directly.

Define Schedule:

In app/Console/Kernel.php, you define your schedule:
php
protected function schedule(Schedule $schedule)
{
$schedule->command(’emails:send’)->daily();
This runs the emails:send command every day.

Run Scheduler:

javascript
php artisan schedule:run
This command checks for due tasks and runs them. You set it up to run every minute in your server’s cron.

List Scheduled Tasks:

javascript
php artisan schedule:list
Shows all tasks you’ve scheduled in your app.

Benefits:

These Laravel Artisan command make it simple to automate tasks in your app. You can send emails, clean up old data, or do any other regular job without manual work.
Remember to set up the scheduler to run every minute on your server for it to work right.

7. Queue Management

Queues in Laravel helps run big jobs in the background. This make your app faster for users. Artisan has commands to manage these queues.

Start Queue Worker:

javascript
php artisan queue:work
This command starts a worker. It processes jobs in the queue. The worker keeps running until you stop it.

Run One Job:

javascript
php artisan queue:work –once
This runs just one job then stops. Good for testing.

List Failed Jobs:

javascript
php artisan queue:failed
Shows jobs that didn’t work right. You can see what went wrong.

Retry Failed Job:

javascript
php artisan queue:retry 5
Tries to run a failed job again. The number 5 is the job ID.

Clear All Failed Jobs:

javascript
php artisan queue:flush
Removes all failed jobs from the list.
This Laravel Artisan command help you manage queues. You can start workers, check on jobs, and fix problems.

Queues are good for:

They make your app feel faster because big jobs happen in the background.
Remember to keep your queue worker running. It needs to be always on to process jobs.

8. Environment and Configuration

Laravel lets you customize settings for different environments. Artisan has commands to manage these.

List Environment Variables:

javascript
php artisan env
Displays current environment variables and values.

Set Environment Variable:

javascript
php artisan env:set APP_ENV production
Sets the “APP_ENV” variable to “production” in your .env file.

Unset Environment Variable:

javascript
php artisan env:unset APP_ENV
Removes the “APP_ENV” variable from .env.

Reload Environment:

javascript
php artisan env:reload
Refreshes .env variables for current request.

Benefits:

These commands help manage separate configs for dev, staging, production, etc. You don’t have to edit .env manually.
Remember to run env:reload if you change .env during a request.

9. Maintenance Mode

Laravel Artisan provides easy way to enable maintenance mode. This restricts access for visitors while you update the app.

Enable Maintenance Mode:

javascript
php artisan down
Puts app in maintenance mode. Visitors see default 503 page.

Disable Maintenance Mode:

javascript
php artisan up
Disables maintenance mode and reopen app to visitors.

Custom Maintenance Page:

You can customize the page visitors see in resources/views/errors/503.blade.php.
Allow IP Addresses:
You can allow certain IP addresses to access app in maintenance mode by editing .env file.

Benefits:

Maintenance mode is useful when updating database, deploying new code, or performing migrations. It avoids disruptions for visitors by putting app in offline state.
Remember to disable maintenance mode when done working on the app.

10. Testing with Artisan

Laravel makes testing easy. You can run tests using Artisan commands. This helps make sure your app works right.

Run All Tests:

javascript
php artisan test
This command runs all your tests. It shows which tests passed or failed.

Run Specific Test:

javascript
php artisan test –filter=UserTest
This runs only the UserTest. Good for checking one part of your app.

Create New Test:

javascript
php artisan make:test UserTest
Makes a new test file called UserTest.php.

Benefits of Testing:

Tests help you find problems before your users do. They make your app more reliable.

Tips:

Remember, good tests make your app better. Use these Laravel Artisan command to keep your app working well.
Testing might seem hard at first. But it saves time in the long run. It helps you build better apps faster.

11. Optimizing Your App

When your Laravel app is ready to go live, you want it to run fast. Artisan has commands to help with this.

Make Routes Faster:

javascript
php artisan route:cache
This command makes your routes load quicker. It’s good to use after you change routes.

Speed Up Config Loading:

javascript
php artisan config:cache
This makes your app’s settings load faster. Use it when you update your config files.

Combine All Files:

javascript
php artisan optimize
This command puts all your app’s files together. It makes your app start up quicker.

Clear Old Stuff:

javascript
php artisan cache:clear
This removes old data from your app’s memory. It can help if things are running slow.

12. Making Your Own Commands

Laravel lets you make your own Artisan commands. This is great for tasks you do often.

Create a New Command:

javascript
php artisan make:command SendEmails
This makes a new file for your command. You can find it in the app/Console/Commands folder.
What’s in a Command File:

Here's an example:

php
protected $signature = ’email:send {user}’;
public function handle()
{
$user = $this->argument(‘user’);
// Code to send email goes here }

Using Your New Command:

Once you’ve made your command, you can use it like this:
avascript
php artisan email:send john@example.com

Why Make Your Own Commands?

Tips for Good Commands:

Remember, Laravel Artisan commands are powerful tools. They can make your work easier and your app run better. Try making some of your own!

Common Mistakes to Avoid:

With these tools, you can make your Laravel app run smoothly and create helpful commands for your team. Keep practicing and you’ll become an Artisan expert!

Conclusion

You’ve learned many handy Laravel Artisan commands for Initializing projects, Managing databases, Running tests, Optimizing performance. Yous should use Artisan commands frequently, They automate tasks and reduce errors and Keep learning new commands as you grow.

Contact N Technolabs today. We’ll show you how to maximize Laravel and Artisan. Together, we’ll build apps users love. At N Technolabs, We offer Laravel Maintenance & Support to keep things running smoothly and we can help you with adding exciting new features, fix tricky bugs, make your app faster and mamy more ways.

Let’s chat and start creating something incredible! Your next great Laravel project is just a call away.

Not sure which Golang framework is right?

Share this story, choose your platform!
Facebook
Twitter
LinkedIn