0% found this document useful (0 votes)
38 views

Lecture 24

Laravel provides support for connecting to and manipulating common SQL databases like MySQL, PostgreSQL, SQLite and SQL Server. It uses Eloquent ORM to interact with databases through CRUD operations. CRUD stands for Create, Read, Update and Delete which correspond to SQL statements like INSERT, SELECT, UPDATE and DELETE. Laravel uses routes, controllers and views to perform CRUD operations. Routes define URLs, controllers contain methods to interact with the database, and views display the data to the user.

Uploaded by

kmani11811
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lecture 24

Laravel provides support for connecting to and manipulating common SQL databases like MySQL, PostgreSQL, SQLite and SQL Server. It uses Eloquent ORM to interact with databases through CRUD operations. CRUD stands for Create, Read, Update and Delete which correspond to SQL statements like INSERT, SELECT, UPDATE and DELETE. Laravel uses routes, controllers and views to perform CRUD operations. Routes define URLs, controllers contain methods to interact with the database, and views display the data to the user.

Uploaded by

kmani11811
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Web Technologies

Working with Databases


Today’s Lecture
Working with Databases
• Configuration
• CRUD Operations
Laravel Databases
• Currently, Laravel provides support for five databases:
– MySQL 5.7+
– PostgreSQL 11.0+
– MariaDB 10.10+
– SQLite (built-in)
– SQL Server (with the sqlsrv driver)
• With Laravel, we can connect to and manipulate databases.
Laravel Databases
• Whenever we create a project in the Laravel, Laravel automatically
makes an entry in the .env file.
• Default connection is with mysql database with the database
exactly like the <projectname>
– Example: project name is “csc336hasnain”, so database name
will be the same (i.e., “csc336hasnain”) as shown in the given
below code.
MySQL Database
MySQL Database
• MySQL is one of the most popular relational database system being
used on the Web today.
• It is freely available and easy to install, however if we have installed
XAMPP/WampServer it’s already there on our machine.
• MySQL database stores data into tables.
– A table is a collection of related data, and it is divided into rows
and columns.
– Each row in a table represents a data record that are connected
to each other.
– Such as information related to a particular person, whereas each
column represents a specific field such as id, first_name,
last_name, email, etc.
MySQL Database
MySQL Database
• Example: structure of a simple MySQL table that contains person’s
general information:
CRUD Operations
Structured Query Language (SQL)
• SQL is a simple, and standardized language for communicating with
relational databases, like MySQL.
• With SQL, we can perform any database-related task, such as
creating databases and tables, saving data in database tables, query
a database for records, deleting and updating data in databases.
CRUD
• CRUD refers to Create, Retrieve, Update, and Destroy records in
database.
• It corresponds to INSERT, SELECT, UPDATE and DELETE queries of
SQL, respectively. Letter Operation MySQL Statement

C Create INSERT

R Retrieve SELECT

U Update UPDATE

D Destroy DELETE
CRUD Operations
INSERT
• Inserting a new employee into a table
INSERT INTO employee (id, first_name, last_name,
email)
VALUES (101, 'Hasnain', 'Iqbal',
'[email protected]’);

SELECT
• Selecting all columns and rows from a table
SELECT * FROM recipe;
• Selecting a specific matching
SELECT * FROM recipe
WHERE type = "spicy";
CRUD Operations
UPDATE
• Updating an employee’s email based on their id
UPDATE employee
SET email = "[email protected]";
WHERE id = 101;

DELETE
• Deleting all rows in a table (without deleting table)
DELETE FROM program;
• Deleting table (completely delete table)
DROP FROM program;
• Deleting a row from a unique id
DELETE FROM program
WHERE id = 1001;
CRUD Operations
There are three steps for performing CRUD operations in PHP: Defining
Routes, Defining Controller, and Defining Views.
• Defining Routes
– It will be used for defining routes corresponding to URLs specified
by the user in the browser’s address bar or generated within the
project.
Route::get('/index','App\Http\Controllers\UserController@index');
• Defining Controller
– In the created controller class, we will write methods that will
perform CRUD operations in the database.
– Data will be passed to views.
function index() {
return view('index');
}
CRUD Operations
• Defining Views
<html>
<head>
<title>My First PHP Website</title>
</head>
<body>
<?php echo "<p>Hello World!</p>"; ?>
<a href="/login">Click here to login</a>
<br>
<a href="/register">Click here to register</a>
</body>
</html>
Example Laravel Project
onlinefishmarket
• Views

• Routes
Route::get('/index','App\Http\Controllers\UserController@index');
Route::get('/register', 'App\Http\Controllers\UserController@register')->name('rs');
Route::post('/register', 'App\Http\Controllers\UserController@store');
Route::get('/login', 'App\Http\Controllers\UserController@login');
Route::post('/login', 'App\Http\Controllers\UserController@match');
Route::get('/home', 'App\Http\Controllers\UserController@home');
Route::post('/add', 'App\Http\Controllers\UserController@storeFish');
Route::get('/edit', 'App\Http\Controllers\UserController@viewFish');
Route::post('/update/{id}', 'App\Http\Controllers\UserController@updateFish');
Route::get('/delete', 'App\Http\Controllers\UserController@deleteFish')->name('delete');
Example Laravel Project
onlinefishmarket
• Controllers
function index() {
return view('index');
}
function register() {
return view('register');
}
function login() {
return view('login');
}
function home() {
$fishData = DB::select('select * from details');
return view('home', ["fishData"=>$fishData]);
function store() {
$uname = Request::input('username');
$pass = Request::input('password');
DB::insert('insert into users (username, password) values (?, ?)', [$uname, $pass]);
return redirect('index’);
.
.
.
.
Summary of Today’s Lecture
Working with Databases
• Configuration
• CRUD Operations
References
• Ch-1, Ch-2, Ch-3; Laravel Up and Running, A Framework for Building
Modern PHP Apps, 2nd Edition, Matt. Stauffer, Oreilly.
• https://laravel.com/docs/10.x/database

You might also like