Blog Posts

Artisan Commands in Laravel

Artisan Commands in Laravel

Artisan Commands in Laravel

Laravel framework provides us with three vital tools for interaction through the command-line namely: Artisan, Ticker, and REPL. Let me explain about Artisan Commands.

First of all, you need to open your command line and go to the project route folder like:
cd var/www/html/laravel-project/
After that, you can execute artisan commands.

Artisan commands – Introduction

Artisan is the command-line interface included in Laravel. It provides a number of helpful commands that can support you while building your application. To view a list of all available Artisan commands in your project, you may use the list command:

php artisan list

Here is a list of few artisan commands:

To start the laravel project:

php artisan serve

To enable caching mechanism:

php artisan route:cache

To view help about any command and view the available options and arguments:

php artisan help serve

There are many artisan commands in laravel. Artisan commands are extremely useful in building laravel applications.

With artisan command you can create a model:

php artisan make:model My-model

And you can also create a controller:

php artisan make:controller MyController --resource

With the use of --resource, it will create basic functions like index(), create(), store() etc.
You can also run the command without --resource, it will create a simple controller without any basic functions.

php artisan make:controller MyController

Without --resource, the controller will look like:

Like that there are so many artisan commands. listcommand will show all of the available commands in your project.

Migration Commands:

Migrations are like version control for your database, allowing your team to easily modify and share the application’s database schema and they are generally paired with Laravel’s schema builder to conveniently build your application’s database schema. Migration is extremely important in laravel to deal with database and migration commands are equally useful in laravel. With the help of these commands, we can create and update tables in the database and we can perform a number of other actions in the database. There are few commands that I am experienced with and I’m going to share the same with you.

To create a table in the database:

php artisan make:migration create_users_table

After this command, a file will be created in the database/migrations folder and you can edit this file according to your requirement. After editing the file, you can run the following command to migrate the table in the database:

php artisan migrate

To Drop all tables and re-run all migrations:

php artisan migrate:fresh

To see all registered routes in your application:

php artisan route:list

make:auth

Creating an authentication in laravel is very easy with artisan command. To create authentication in laravel  you can simply use the following command:

php artisan make:auth

Before running make:auth command front view:

Before executing make:auth command as you can see there is no Login and Register Links in view.

After executing make:auth command front view:

After this single command, it will create view files like layouts,  auth files and HomeController for your Front-end. This command also sets routes for authentication. In short, it will do everything which is necessary for a user to register and log in. It will also set ‘after login view’ for your application. After this command, you need to run the migration command to complete the process:

php artisan migrate

This command creates tables in the database. Now you can register, login and logout in your application.

After the Login view:

All these views are created by make:auth command.

Do share this article with your friends/co-workers if you find it helpful!

« Previous Post

Remove public from url in Laravel 5.7

Restful API in Laravel using Passport With Authentication

Next Post »

Restful API in Laravel using Passport With Authentication

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.