Sei sulla pagina 1di 26

Getting Started with Laravel

By Juned Laliwala

Short description of Laravel and PHP


Introduction to laravel
Concept of Artisan Tool
Database Migrations
Models and Controllers

@2016 Attune World Wide All right reserved.


www.attuneww.com

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

Getting started with Laravel

Last Updated: November 2016

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.

1.2. Intended Audience


This guide is particularly intended for the developer who are having the basic knowledge
of the PHP language. We are going to use the laravel framework to work with the PHP.

1.3. Revision History


This is the first version of the Laravel and PHP. Complete introductory information will
be available in this guide.

Getting started with Laravel

Last Updated: November 2016

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

Scheduler and many more.

Getting started with Laravel

Last Updated: November 2016

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.

Creating Laravel Project


Laravel project can be created using the command as given below:
composer create-project laravel/laravel --prefer-dist attune-service
Now let us take a close look at the command shown above.
1. composer: Package manager for downloading the packages intended for the
project
2. create-project: Command for creating the project.
3. laravel/laravel: Suggesting that it is the laravel project.
4. --prefer-dist:
5. attune-service: Name of the project.

So, all the dependencies necessary for the laravel project will be downloaded and
installed into our project folder.

Getting started with Laravel

Last Updated: November 2016

Page

5 of 26

Project Folder Structure

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

Getting started with Laravel

Last Updated: November 2016

Page

6 of 26

Configuring Project Environment


The project environment can be configured using the file namely .env which is
residing in the main laravel project folder. In this section, we will focus on creating the
database connection (MySql for this section) on our local machine using the
environment file.

Fig: .env file


From the above figure, we can see that we are connecting our Mysql Database to our
laravel project. The default host is being set to 127.0.0.1 which means that when we
type this into the url then also the application will be running. The Default fort for the
application is 3306.
The name of the database is laravel. This database is created inside the sql control
panel. So we are going to use the laravel database for the laravel project.

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.

Getting started with Laravel

Last Updated: November 2016

Page

7 of 26

Migration Concept in Laravel


Migration in laravel is nothing but the database connection to the laravel project. In this
section, we will come across various functionalities with which the database can be
worked. With the help of the migration, we can create table, delete a table, work with the
table, updating row/column.

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

Last Updated: November 2016

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

Last Updated: November 2016

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

Last Updated: November 2016

Page

10 of 26

Fig: Details Table

Fig: Employee Table

Getting started with Laravel

Last Updated: November 2016

Page

11 of 26

Rolling Back and Reset the Migrations


In the previous section, we have seen the way to create a migration and after creation we
have just imported automatically into our database server. Now in this section, we will
come across the various tasks that we need to perform the rolling back to the previous
action.
Artisan tool help us to perform this action. So let us see the command for the rolling
back the migration.
php artisan migrate:rollback

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.

2. Php artisan migrate:reset:

Getting started with Laravel

Last Updated: November 2016

Page

12 of 26

Concept of Database Seeder


Seeder are actually the pre-data that we need to use for the future with the database.

Creating Database Seeder


Seeder in a laravel can be created using the artisan tool.
php artisan make:seeder EmployeeTableSeeder
In the command above, we are going to create a seeder named EmployeeTableSeeder.

Now just navigate to the project folder. Inside the seeds folder, we will see that our
created EmployeeTableSeeder has been included.

Fig: Simple EmployeeTableSeeder

Getting started with Laravel

Last Updated: November 2016

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

Last Updated: November 2016

Page

14 of 26

php artisan db:seed


So this command will eventually load all the seeds that have been defined for the
project.

Just run or refresh the database server.

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

Now, inside the CLI we need to give the command:

Getting started with Laravel

Last Updated: November 2016

Page

15 of 26

php artisan db:seed --class=EmployeeTableSeeder

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.

Getting started with Laravel

Last Updated: November 2016

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.

Getting started with Laravel

Last Updated: November 2016

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.

Fig: Employee Model


From the above figure, we can see that inside the detail method we are returning Detail
Model.
Similarly, now we will create employee method inside the Detail model to demonstrate
the relationship between them.

Getting started with Laravel

Last Updated: November 2016

Page

18 of 26

Fig: Detail Model


Now, we can see that we are using belongsTo() function to show that the Employee
Class is belonging to this particular model.
So, we have now configured from both the side of the relationship. This means that
suppose we have the details of the employee, then we can get the employee and in the
similar way if we have the employee then we can get the details of the employees.

Getting started with Laravel

Last Updated: November 2016

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.

Getting started with Laravel

Last Updated: November 2016

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.

Concept of group in routing


Group as the name suggest we can include as many routes that are required for the
particular group. The group name can be specified as per the users requirement. In our
case we will create the group name as api which will be working also as the prefix for
the groups that we have created.

Getting started with Laravel

Last Updated: November 2016

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.

Testing Concept in Laravel


Testing is used in laravel for performing any of the test that we need to do for any of the
particular task. For testing, we need to navigate to the tests folder inside the project
folder. Just open the ExampleTest.php file and let us create a new test for the
application.

Getting started with Laravel

Last Updated: November 2016

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.

Getting started with Laravel

Last Updated: November 2016

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

Getting started with Laravel

Last Updated: November 2016

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.

Using resource method

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.

Getting started with Laravel

Last Updated: November 2016

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.

Getting started with Laravel

Last Updated: November 2016

Page

26 of 26

Potrebbero piacerti anche