You can download the source code of this tutorial here: https://www.techjblog.com/index.php/laravel-tutorial-for-beginners/
Laravel is a very powerful framework that follows the MVC structure. It is designed for web developers who need a simple, elegant yet powerful toolkit to build a fully-featured website.
This tutorial explains the basic use of this framework by building a simple blogging system. But before we start, there are some software we need to install.
XAMPP is a free and open-source cross-platform web server solution stack package developed by Apache Friends. It contains MySQL, phpMyAdmin, PHP, and Apache server.
It combines everything you need for PHP web development in one place and spares you the time for installing and configuring each of them individually.
Composer is a dependency manager for PHP, and it helps you install Laravel and all the other dependencies that you need for building a website. Remember, you need to install XAMPP or PHP first, or the composer installer will not be able to find
The good news is, if you are a student, you will be qualified for a free copy with your student email. However, you do need to verify your student status every year.
First, let us create a new empty PHP project in PhpStorm. In this tutorial, we’ll call it “Laravel_8_Tutorial”.
Now, we need to install Laravel for our project. Open “Terminal”, it should go to the root directory of the project. Run the following commands.
Download the Laravel installer:
composer global require laravel/installer
This command will automatically install the latest version of the laravel installer.
Next, we can create a new Laravel application in the project directory:
If you are planning on creating multiple apps, then put each app into an independent folder, for example:
laravel new blog laravel new gallery
This installation process will take a few minutes to complete.
Once it is done, this is what you should get:
Here I will briefly introduce the directories that we need to use in this tutorial since some might be a little confusing for beginners. If you are interested, you can read the Official Documentation, which explains the function of each directory in detail.
This folder contains the entire source code of our project. It includes events, middleware, exceptions, and so on. But, for now, we only care about models and controllers.
As the name impies, it stores all configuration files for our project.
database directory is where we put all the seeds and migration files. They determine the structure of the database. If you wish, you may also use this directory to hold an SQLite database. But I don’t recommend using SQLite for huge projects.
This directory holds the index.php file, which is the entry point for all requests. Also, we need to put all static files (CSS and JS) into this folder.
route directory contains all URL declarations for our project. By default, there are four route files:
channels.php. For this tutorial, we only need to understand
This is where we put all the controllers for our project. All of the logic to handle requests entering your application will be placed in this directory.
Laravel 8 now has an individual folder for models.
Models directory contains all of your Eloquent model classes. The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
All the environment configuration in Laravel are stored in the
.env file in the root directory. To make our project function properly, there are some changes we need to make.
First, let’s define the URL for our project. Since we are in development, the URL should be
If you installed XAMPP, we can use the MySQL database that comes with the software. We need to create a new databse for our project first. Open XAMPP control panel:
Click on the “Admin” button for MySQL. This will take us to phpMyAdmin, which is the administration panel for MySQL databases.
We create a new database here, and then, we tell Laravel how to access this database. Go back to the
.env file, and find the database section (the one with the
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_8_tutorial DB_USERNAME=root DB_PASSWORD=
Setting an application key is essential to the safety of our project. If the application key is not set, your user sessions and other encrypted data will not be secure.
The following command will set the application key for you:
php artisan key:generate
This configuration is not about the environment, but it is still very important. In order to make sure Laravel will find the media files (images, videos…) we uploaded, we need to create a symbolic link in the
public directory with this command:
php artisan storage:link
Finally, it’s time to start our server. We have two choices, we can run the built-in local development server:
php artisan serve
Or we can use the Apache server from XAMPP, but you might need to change the “
DocumentRoot” setting in the configuration file. Open the XAMPP control panel again. Click the “Config” button and open
DocumentRoot and change its path to the
public folder of your project.
Once you are done, restart the server. Open the browser and go to http://localhost/. You should see the Laravel welcome page.
Next, we’ll take a look at routing in Laravel.