Lecture 24
Lecture 24
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