Before knowing about php artisan commands in Laravel you should install laravel, which is already described in our previous posts. You can follow any of these posts as per your requirement.
- Installing Laravel 5.7 with XAMPP on Windows
- Installing Laravel 5.5 with XAMPP on OSX
- How to Install Laravel with MAMP on Mac OSX
- Installing Laravel 5.5 with XAMPP on Linux
Let’s understand about php artisan & it’s uses in laravel.
php artisan in Laravel Framework is one of it’s amazing feature. It saves a lots of time in developing a laravel Website/Application.
What is php artisan ?
php artisan is the default command line interface in laravel. It consists of number of commands which helps in developing a laravel application easily. If you have already installed laravel, then you must have an general idea about the following artisan command which starts laravel application for execution.
php artisan serve
Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.
Here my laravel project is named as laravel which is placed in htdocs folder. Then in terminal I entered into laravel directory then typed php artisan serve command. As you can see laravel development server started with a url 127.0.0.1:8000. Here 8000 is port no. It may different in your case.
Now Let’s go through some php artisan commands.To get list of available php artisan commands in laravel run the following command in Terminal.
php artisan list
Here we will know about some commands which will be very helpful for you.
php artisan help
It displays help for a command
Using php artisan make command we can create a controller, model & migrations etc. automatically in our application.
The list of artisan make commands in laravel are as follows.
make:auth
make:channel
make:command
make:controller
make:event
make:exception
make:factory
make:job
make:listener
make:mail
make:middleware
make:migration
make:model
make:notification
make:observer
make:policy
make:provider
make:request
make:resource
make:rule
make:seeder
make:test
Let’s go through some commands.
php artisan make:controller controllername
//Replace controllername with your controllername as below
php artisan make:controller LoginController
It will create a new controller named LoginController
in application in path app/http/controllers
.
You can see the controller LoginController.php
with code is created in the application by the command. So no need to create a file in controller folder. This is really awesome feature in Laravel.
php artisan make:controller controllername --resource
//Replace controllername with your controllername as below
php artisan make:controller CrudController --resource
It will create a resource controller named CrudController in application. You can see in below snapshot that CrudController has been created in applications in path app/http/controllers.
So in resource Controller following methods will be created be default.These are index()
, create()
, store()
, show()
, edit()
& destroy()
. You can see these methods in following code.
Using these method one can perform CRUD (create,Read,Update & Delete) operations easily by editing the codes.
php artisan make:model Studentmodel
It will create a model in app directory as Studentmodel.php
php artisan make:middleware TestMiddleware
It will create a middleware in the application in path app/http/middleware/
php artisan make:migration alter_users_table
Here users is the existing table name. A migration file will be created in app/database/migrations
.
Now you can edit the migration file by writing code in up()
& down()
to perform database operations on that table.
After that by running the following command the codes will execute & respective fields will be migrated.You can check in your database.
php artisan migrate
Now let’s go through some artisan commands in brief
php artisan make:seeder LoginsTableSeeder
The above artisan command a new database seeder class will be created..
php artisan make:request StoreNews
The above artisan command will create a new form request class will be created in app/Http/Requests folder.
php artisan make:middleware CheckBirthDate
The above artisan command a new middleware class will be created in app/Http/Middleware folder.
php artisan make:mail TicketBooked
The above command will create a new email class.
php artisan -v
It will show the current laravel version along with it will show all the available commands in php artisan.
To clear cache in laravel 5.7 run the below artisan command
php artisan cache:clear
To Clear Route cache run the below artisan command:
php artisan route:cache
To clear view cache run the below artisan command:
php artisan view:clear
To clear config cache run the below artisan command:
php artisan config:cache
To generate key in laravel run the following command:
php artisan key:generate
The above artisan commands are really helpful. After knowing the uses of these commands you can explore for other artisan commands.
Happy fun with php artisan commands in Laravel!