Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By Juned Laliwala
Contents
Preface
1.1. About This Guide
1.2. Intended Audience
1.3. Revision History
Introduction to Laravel
What is Laravel?
Features of Laravel
Installation of Laravel
Creating Laravel Project
Project Folder Structure
Concept of Artisan
Configuring Project Environment
Migration Concept in Laravel
Creating Migration
Rolling Back and Reset the Migrations
Concept of Database Seeder
Creating Database Seeder
Models in Laravel
Creating Model
Creating Routes
Concept of group in routing
Testing Concept in Laravel
Creating Controllers
Using resource method
Conclusion
Page
2 of 26
Preface
1.1. About This Guide
In this guide, we will come across the main framework that is running in the market
named Laravel. This is the advanced framework with which we can easily work with
the command line interface, database migration is easier and many more aspects.
Page
3 of 26
Introduction to Laravel
What is Laravel?
Nowadays, many of the developers or the common people are on day-to-day activities
finding the shortcuts for performing any of the particular task. The reason behind this is
just the saving time concept. So for keeping the same track, the new framework named
Laravel came into action. Laravel, the name is typical, but the concept and the syntax
behind it is very easy. Laravel is Web Application Framework with expressive syntax.
Authentication, Sessions and Cookies concepts are very easily focussed using the laravel
project. In other words, various functionalities for the security purpose are being created
in a very simple and smooth way.
Features of Laravel
Bundles
Class Autoloading
Cashier
Eloquent ORM
Blade CLI
FlySystem
Query Builder
Migrations
SSH
Application Logic
IoC Containers
Socialite
RESTful controller
Form Request
Page
4 of 26
Installation of Laravel
The Application or the project that we are running can be made laravel dependent using
one of the most well-known package manager named composer. With the help of the
composer, we will create a laravel project.
So, all the dependencies necessary for the laravel project will be downloaded and
installed into our project folder.
Page
5 of 26
As we can see from the above figure that all the required dependencies and files have
been installed successfully.
Concept of Artisan
Artisan is the tool used for the CLI that comes in-built with the laravel in order to get the
work done quickly.
If we want to just see that the content inside the artisan then give the command shown
below:
php artisan
Page
6 of 26
If we have set the username and password for the database then we need to specify it
inside the env file, otherwise the connection will not be established.
Page
7 of 26
Creating Migration
The command for creating migration is:
php artisan make:migration create_employees_table --create=employee
As above, we can also create the same for the description of the product.
So, both the products and descriptions migration has been created.
Now, when both the products and descriptions table have been created then we can just
navigate to the project folder to see that files for the migration has been created.
As we can see from the above figure that the migrations that we have created are now
created as a file inside the migration folder.
Getting started with Laravel
Page
8 of 26
The general method for the migration is the up method and down method inside the
class name. This class name will be the same as migration that we have created using
CLI.
Now, let us add some columns to our table. This addition will be done under up method
as we are adding some functionalities to the table.
Fig: create_employees_table.php
As we can see from the above figure that we have added one column named name into
our products table.
Fig: create_details_table.php
From the above figure, we can see that we have applied two more columns to this
migration. Employees ID has been defined as a foreign key using unsigned function.
In this table, we have added an extra feature which is going to reference the id field
onto the employees table that has been created.
Getting started with Laravel
Page
9 of 26
After the table has been added with the columns,we need to update the same into the
main laravel project and this can be done using the command shown below:
php artisan migrate
When for the first time, we might get the error that the database not found. So dont
worry, just navigate to the .env file and change the name of the database. This
database that you are giving must be mandatorily created inside the mysql panel.
As we can see that the message has been displayed saying that migration table has been
created. We have as usual made the two changes in both the table so we are getting the
names of the table that we have updated.
As we have created the laravel migration inside the laravel database that we have
defined inside the environment file. So we will just see the list of the tables that have
been created are displayed as shown in the figure below:
So, the database has been created successfully inside our local sql server.
Getting started with Laravel
Page
10 of 26
Page
11 of 26
Whenever this command is invoked, all the tables will be removed from the migration as
shown in the figure below:
Another option is reset. This feature will remove the migration forever.
1. Php artisan migrate: this will bring back both the roll backed migration.
Page
12 of 26
Now just navigate to the project folder. Inside the seeds folder, we will see that our
created EmployeeTableSeeder has been included.
Page
13 of 26
Now along with it, we will also see the Database seeder that has been included
automatically. Now just inside this database seeder file include our newly created seeder
as shown in the figure below:
In the figure shown above, we are importing our newly created seeder using the call
function.
Fig: EmployeeTableSeeder
From the above figure, we can see that we are currently passing the static values inside
the table named employee.
In the next line, we can see that we are passing the dynamic values that is the values that
are passed when the user types into the form.
To run this seeder, we need to give the command:
Getting started with Laravel
Page
14 of 26
So we can see that, the two values has been inserted into the database.
Another option for running any particular seed is by removing the $this line from the
Database seeder
Page
15 of 26
So, this command will only see the EmployeeTableSeeder. It will not seed any other
seeder.
Refresh the sql server to see the result as shown below:
So, we can see that again two more values have been added to the database. So our seed
is working perfectly.
As we know that we have just removed the $this line which means that
EmployeeTableSeeder will not be invoked unless it is mentioned inside the command.
So when we will just run php artisan db:seed, no updation in the table will be done
as we have not specified the name of the seeder.
Page
16 of 26
Models in Laravel
In the previous section, we have come across creating the migration and accordingly we
have created seeds for the migration. Models will be the totally coding work. So we will
now create Employee Model for the employees and Detail Model for getting the details
of the employees. As similar, we will use the artisan to create the model.
Creating Model
As in the previous section of the migration, we have seen that we have removed the user
and password reset migration. The same is the case with the model, there will be the
default user model, so we are going to delete the user model that have been defined.
The models are residing inside the app folder. So let us delete the User.php file. So
now we do not have any single model. So let us create a model using artisan and make
namespace as:
php artisan make:model Employee
So we have created a model named Employee and Detail.
Now just navigate to the project folder, we will see that both the models have been
created. Now with the help of the model, we will show the relation between the
Employee and their corresponding details that we have already created in the previous
section.
Now, inside the Employee Model, we are going to create detail method which is showing
the relationship between the Employee and their corresponding details.
Page
17 of 26
Inside this method, we are going to return object hasMany(). This hasMany function
takes many of the parameters as per the functionality of the project.
Page
18 of 26
Page
19 of 26
Creating Routes
Till now, we have seen that we have created the model and also we have created the
relationship between them. So now we need to tell the application that to do some task
whenever it sees some HTTP request. For that purpose we need to create route.
Routes can be configured inside the Http folder of the project folder. Inside this folder
we will find routes.php file.
The above figure represents the default routing file for the laravel project. So from the
above figure, we can see that we are using Route parameter for invoking routing. After
that we have to pass the method for Getting, Posting or whatever according to the
requirement.
So let us make some routing for the Employee page. This page will be responsible for
handling the employees and the related details.
Page
20 of 26
So from the above figure, we can see that we have created one more route named
employees which means /employees in the terms of the browser. Along with this
route, we are passing the function. This function will be responsible for handling the
particular page that we are working on. Now inside this function, we are returning the
list of all the employees that we have already created in the model section.
Now, there is an advanced concept in laravel with which we can deploy our system using
the artisan tool.
php artisan serve --host=192.168.15.42
So, whenever from any of the machine you will give the url as
http://192.168.15.42/employees then all the lsit of the employees will be displayed on
to the screen.
Page
21 of 26
The reason for creating the group is to make a different view as compared to other
group. In the previous, we have seen that we have passed the url as
http://192.168.15.42/employees. But now we have included the employees section
inside the group. This group has been given the prefix as api so now our url will be
changed to http://192.168.15.42/api/employees. The Results will be the same.
Page
22 of 26
From the example shown above, we can see that we have created one new test named
testEmployeeList to simply check the list of the employees that we have created in the
previous section. Inside this function we are passing the route(employees), which
means that we are trying to get the complete http request for the list of the employees.
Page
23 of 26
Creating Controllers
Controllers are normally used to control the overall functionality of the laravel
application. With the help of the artisan tool, the controllers are created very easily.
Controllers are main element of any of the project of any framework.
Let us create a controller using artisan tool.
php artisan make:controller EmployeeController
So as we can see that we have created two controller one for the Employee and one for
details of the Employee. EmployeeDetailController will be responsible for showing the
relationship between the Employee and the corresponding details of them.
The controllers are created inside the app\Http\Controllers folder.
These controller already extend the built-in controllers of the laravel. Inside this
controller file, we will see that there are built-in functionalities of performing CRUD
operations.
Now just navigate to the routes file.
Fig: routes.php
Page
24 of 26
Laravel comes in-built with the resource which will directly create the relationship
between the controller that we have created and the model.
Route::resource(employees,EmployeeController,[only
store,update]);
=>
[index,
The above syntax means that whenever we are in the location /employees then we are
going to refer the EmployeeController for that particular model. The only inside the
array will have only the names of the method that have been created inside the
controller that we want to use.
Similarly, whatever the functions like add, delete, update can be done easily using the
EmployeeController for the Employee model.
Now, let us just see the basic usage of the resource method. As we know that whenever
we are creating the controllers there are various methods that are being created
automatically even though we do not need it in some of the cases. So for our application
for the time being, we will be needing the following methods as:
1. index() method for getting all the employees list
2. store() method for creating new employees for the application
3. update() for updating the employee details and so on.
Remember that, resource is simply binding between the path and the
controller that we have created.
So,we can see that whatever methods we have created inside our controller can be easily
interpreted based on the resources defined.
Page
25 of 26
Conclusion
In this book, we have come across making an laravel application. We have also seen that
we can easily create code as it is dependent of easy-to-code functionality. We have also
seen the database migration concept with which our application is totally dependent on
the database queries. In this book we have also seen the concepts of controllers and also
created the controllers in our laravel application. In the next version of this book, we will
come across various advanced concepts of the sections that we have come across.
Page
26 of 26