
批改狀態(tài):合格
老師批語(yǔ):繼續(xù)
第9行到14行
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=php
DB_USERNAME=root
DB_PASSWORD=123456
修改成自己的數(shù)據(jù)庫(kù)賬戶
<?php
namespace App\Http\Controllers;
use DB;
class Home extends Controller{
// 數(shù)據(jù)庫(kù)查詢
public function get(){
echo '<pre>';
$res = DB::select('select * from users');
print_r($res);
}
//數(shù)據(jù)庫(kù)更新
public function gxupdata(){
$res = DB::update('update users set name="我知道,最好的語(yǔ)言:php25歲了" where id=4');
var_dump($res);
}
//數(shù)據(jù)庫(kù)新增
public function xzupdata(){
$res = DB::insert('insert users(`name`,`email`,`password`)values("mignzi","ad@s.com","aginogsg")');
var_dump($res);
}
//刪除數(shù)據(jù)
public function delete(){
$res = DB::delete("delete from users where id=2");
var_dump($res);
}
public function index(){
// 加載v視圖文件
$res = DB::select('SELECT * FROM `jizhang`');
$lists = [];
foreach ($res as $val){
$lists[] = (array)$val;
}
$data['result'] = $lists;
// print_r($val);
return view('v', $data);
}
}
路由代碼:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('v');
// echo date('Y-m-d h:i:s');
});
Route::get('/article/p/aaa', function () {
// return view('v');
echo date('Y-m-d h:i:s');
});
Route::get('home/index','Home@index');
Route::get('/db','Home@get');
Route::get('/dbgx','Home@gxupdata');
Route::get('/dbxz','Home@zxupdata');
Route::get('/dbsc','Home@delete');
文件代碼:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>數(shù)據(jù)庫(kù)操作</title>
<link rel="stylesheet" type="text/css" href="/layui/css/layui.css">
<script src="layui/layui.js"></script>
</head>
<body>
<table class="layui-table">
<thead>
<tr>
<td>ID</td>
<td>金額</td>
<td>賬戶</td>
<td>成員</td>
<td>備注</td>
<td>成員id</td>
</tr>
</thead>
<tbody>
<?php foreach ($result as $val): ?>
<tr>
<td><?php echo $val['id']; ?></td>
<td><?php echo $val['jine']; ?></td>
<td><?php echo $val['zhanghu']; ?></td>
<td><?php echo $val['chengyuan']; ?></td>
<td><?php echo $val['beizhu']; ?></td>
<td><?php echo $val['yonghuid']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
效果:
總結(jié):本節(jié)課學(xué)習(xí)了 laravel框架 的mvc 與數(shù)據(jù)庫(kù)查詢刪除修改的操作,更詳細(xì)了學(xué)習(xí)了laravel框架。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)