Follow Us:
PHP /

Artisan commands in laravel for beginner

Created Date: 2 days ago
Updated Date: 27th May, 2023

Laravel is a widely used PHP framework that runs on Apache web servers and boasts a large and active community.

One of Laravel's standout characteristics is its robust command-line interface, called artisan, which provides developers with a suite of powerful tools for building web applications. In this post, we will discuss some of the most important artisan commands for Laravel beginners.

First, let's define what artisan is, as per the official Laravel documentation:

"Artisan is the command line interface included with Laravel. Artisan exists at the root of your application as the artisan script and provides a number of helpful commands that can assist you while you build your application."

Read more about artisan through this link.

So basically artisan is just a collection of command-line tools that can facilitate various aspects of web application development.

1- Launch Local Development Server:

To start a development server to serve your application locally, and you can access it through http://localhost:8000

php artisan serve

2- Launch local development Server With Specific Port:

The default port is 8000 to serve your application, you can change it using the command below:

php artisan serve --port=800

3- Create Model:

To create a model in Laravel, you can use the following artisan command:

php artisan make:model {model name}

IIt is important to note that the best practice is to name the model in the singular form, as the corresponding database table will typically be named in the plural.

4- Create Model With Migration:

To create a model with a corresponding migration file, use the following artisan command:

php artisan make:model {model name} -m

What is Migrations?

Laravel documentation defines migrations as below:

"Migrations are like version control for your database, allowing your team to define and share the application's database schema definition. If you have ever had to tell a teammate to manually add a column to their local database schema after pulling in your changes from source control, you've faced the problem that database migrations solve."

So basically migrations are used to version control your application's database schema definition and facilitate collaboration among team members.

Read more about migrations through this link

5- Add Migration to a specific table:

php artisan make:migration {name of migration} --table={name of table}

This command generates a new migration file that can be used to modify the specified table.

6- Add a specific migration to a database:

php artisan migrate:refresh --path=/database/migrations/{name of migration}.php 

This command refreshes your database by rolling back all migrations and then re-running only the specified migration file.

7- Make Controller:

php artisan make:controller {controller path / name}
php artisan make:controller Admin/Products
php artisan make:controller Website/Products

8- Make Controller with resources:

php artisan make:controller {controller path / name} -resource

This command will generate the following methods:

1- index => get

2- create => get

3- store  => post

4- show  => get

5- edit  => get

6- update => patch | put

7- destroy => delete

9- Commands To Be Used Before Backup:

php artisan config:cache
php artisan cache:clear
php artisan view:clear

Before backing up your Laravel application, it is important to run the above artisan commands.

php artisan optimize:clear

The above command will purge caches for [events, views, cache, route, confige, compiled]

Share the post:

Tags:
php
laravel
artisan
beginner