You are on page 1of 14

Course Code: Track Elective 3

Course Title: Advance Web Development using Laravel


Credit Units: 3

Overview/Introduction

This course provides an overview and discussions of the web development framework
Laravel. This course include: brief history of web frameworks, introduction on PHP
frameworks, the development framework Laravel, Installation via composer, initial
configuration, directory configuration, Laravel file Structure, local development
environment, Git bash, PHP artisan commands, Routes, Middleware, CSRF protection,
Controllers, Requests, Responses, Views, Blade Templates, Session, Validation, Error
Handling, Models, Database Migrations, Eloquent, and Simple App Testing. This course
focuses on developing a web application using Laravel framework with CRUD+S – create,
read, update, delete and search functionality as final output in the course subject.

Instructor: Leo P. Paliuanan


leopaliuanan@csu.edu.ph

UNIT 4 – Diving deeper into the basics of Laravel

Lesson/Topics:

a. Working deeper with models


b. Working with database migrations
c. Retrieving data from the database table using models and controllers
d. Display data from the database using view

Unit overview

Unit4 describes and provides familiarity on how to use Eloquent and model classes and
how to use them. It also details out working with migrations and on how to retrieve data
from tables using models

Learning Outcomes

At the end of this unit the student should be able to:

 Illustrate and demonstrate knowledge on how to create models


 Demonstrate knowledge in creating database migrations
 Describe and demonstrate knowledge on how to retrieve data from a database
table using models
 Illustrate and demonstrate knowledge on how to display data using views and blade
templates
To start with, we need to create a database and connect our Laravel project.

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable


to interact with your database. When using Eloquent, each database table has a
corresponding "Model" that is used to interact with that table. In addition to retrieving
records from the database table, eloquent models allow you to insert, update, and
delete records from the table as well.

To get started, lets us


create a database first.

1. Create a database
“db_plnewapp”

Connect Database
“db_plnewapp” to Laravel
Project.

2. Go back to
VSCode, then
locate .env file then
look for the line
DB_DATABASE

3. Change laravel
with the name of
the database we
created
“db_plnewapp”

4. Save…
A. Working deeper with models

1. Create a model “Product”

To get started, let's create an Eloquent model. Models typically live in the app\Models directory
and extend the Illuminate\Database\Eloquent\Model class. You may use
the make:model Artisan command to generate a new model:

If you would like to generate a database migration when you generate the model, you may use
the --migration or -m option:

On terminal type in php artisan makeLmodel Product –m


Then press enter

Note: it will create a migration for the database table Product


Let’s create Controller for our products

Steps:

1. Open terminal
2. Type in php artisan make:controller ProductsController –r
3. Press enter and wait to finish

When the process is done you should be able to see the screen above

Once the model is created we can


locate the model by clicking on the

app > Models

You should be able to see that we have


the fie “Product.php”

This only indicates that we have


successfully created the model for the
product.

By creating the model using the –m


option it will create a migration file for
the table “products” as well, and it is
located at
2. goto Database > migrations

The next thing to do is to setup our table schema, we need to add the fields for table
products.

3. Open the file 2021_09_23_094043_create_products_table.php


4. Under public function up(), Add the following, just after the $table->id();

$table->string(‘pid’);

$table->mediumText(‘barcode’);

$table->mediumText(‘name’);

$table->mediumText(‘type’);

$table->date(‘expiration’);

$table->mediumText(‘physical_count’);

It should look like this

B. Working with database migration, we will now run a database migration


1. Open terminal and type in php artisan migrate, then press enter…
You should be able to see that the table products has been created…

1. Check database changes via browser localhost phpmyadmin

Open a web browser and type in on the address bar http://localhost/phpmyadmin,


you should be able to recognize that there is a new table “products” added

2. Insert dummy data into the table “products”

Steps

1. Click insert tab


2. Enter record into the column value
3. Click go to save
You should be able to see the results after adding some dummy records into our table
products.
C. Retrieving data from database table using models and controllers.

In our case, we will make use of a model and controller to re-route our display
going to a view for the user to see the data from our database table “products”

1. Run php artisan route:list on terminal to check available routes

Currently we have the following routes available on the column URI, we will create a
new route for our products for the display.

2. Goto routes > web.php

3. Open web.php and type in the codes on the very bottom.

Route::resource('products','ProductsController');
4. Run php artisan route:list on terminal again, type in php artisan route:list,
then press enter…

You should have


routes available for
ProductsController
5. Goto ProductsController and open it and focus on the public function
index() …

6. On the public function index()

Add the following codes:

$p = Product::all();
return $p;

7. Open terminal, type php artisan serve, then press ctrl + click on the
address http://127.0.0.1:8000 to open on browser, then add /products on
the url.
You should have the following output on your browser …

This indicates that we are able to fetch the data coming from our database table
“products”

D. Display data from the database using view

Create a view for the products


1. Goto resources > views
2. Create a new file and name it as products.blade.php
3. Open products.blade.php and add the following codes as an example:

<!DOCTYPE html>
<html>
<title>User Page</title>
<body>
<table class="table">
<thead>
<tr>
<th>Barcode</th>
<th>Name of Product</th>
</tr>
</thead>
<tbody>
@foreach($p as $d)

<tr>
<td>{{$d['barcode']}}</td>
<td>{{$d['name']}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>

It should look like this:


4. Go back to the browser then refresh the application again, you should have the
output below:

Reference

https://laravel.com/docs/5.0/artisan#introduction

https://laravel.com/docs/8.x/requests#interacting-with-the-request

https://laravel.com/docs/8.x/routing

You might also like