X
    Categories: Laravel

What are 10 Useful Artisan Commands Used During Development in Laravel

Laravel is an amazing PHP framework loved by many developers. One of its best tool is Artisan — a command-line interface that makes development faster and easier. If you are starting a new project or maintaining an existing one, using Artisan commands will save you time and effort.

In this article, we will explore 10 useful Laravel Artisan commands that you should definitely know. I’ll explain each command with simple examples, so you can start using them right away!

1. php artisan list

First things first, if you want to see all available commands in your Laravel app, run:

php artisan list

This command lists every Artisan command that your app supports. It’s a great way to explore what you can do with Artisan.

2. php artisan serve

When you want to run your Laravel app locally, this command helps:

php artisan serve

It starts a local development server and gives you a URL to open your app in the browser. It’s quick and easy, no setup needed!

3. php artisan make:controller

If you want to create a new controller, use this command:

php artisan make:controller ExampleController

This generates a new controller file automatically. It saves you from writing boilerplate code manually.

4. php artisan make:model

Need a new Eloquent model? Just run:

php artisan make:model Example

You can also create a migration to go with the model by adding -m:

php artisan make:model Example -m

This way, your database schema and model stay in sync easily.

5. php artisan migrate

After you have created your migration files, apply those changes to your database by running:

php artisan migrate

This updates your database tables according to your migrations.

6. php artisan migrate:rollback

Sometimes, you might want to undo the last database changes. This command rolls back the most recent migration batch:

php artisan migrate:rollback

It’s very useful when you want to test or change your database structure.

7. php artisan db:seed

If you want to fill your database with sample data during development, Artisan makes it easy:

php artisan db:seed

This runs your seeders and populates your tables with fake or sample data.

8. php artisan cache:clear

Caches can sometimes cause confusing issues during development. To clear all cached data, run:

php artisan cache:clear

This ensures you’re always seeing the latest version of your app.

9. php artisan route:list

To view all the routes registered in your application, this command is handy:

php artisan route:list

It shows the HTTP methods, URIs, controllers, and middleware for each route. Great for debugging!

10. php artisan tinker

Finally, sometimes you want to quickly test code or interact with your app’s database. Laravel’s Tinker provides an interactive shell:

php artisan tinker

For example, you can create a new user right from the terminal:

>>> App\Models\User::create(['name' => 'Alice', 'email' => 'alice@example.com', 'password' => bcrypt('secret')]);

It’s perfect for quick experiments without writing a full script.


Why Use Laravel Artisan Commands?

Using these commands can speed up your workflow. Instead of writing repetitive code, Artisan automates common tasks. Furthermore, commands follow Laravel best practices, helping you write clean and maintainable code.

Plus, Artisan commands make it easier to manage your app’s development flow, database, routing, and caching. This means fewer errors and more time to focus on features your app really needs.

Conclusion

Mastering these 10 Laravel Artisan commands will help you become a more effective developer. From generating files and managing databases to debugging routes and testing code, these commands cover essential parts of Laravel development.

Start using these commands today, and watch how they make your development fast, fun, and efficient!

greattechblog: