
批改狀態(tài):合格
老師批語:如果前面掌握了, 這里應(yīng)該是水到渠成的事
$result['res']=$data;
, return view('home/goods',$result);
,傳輸?shù)絞oods.blade.php頁面中的數(shù)據(jù)是echo $res
在當(dāng)前文件夾下打開終端,輸入以下命令
新建indexCon控制器
php artisan make:controller home/goodsCon
新建goodsCon控制器
php artisan make:controller home/indexCon
新建控制器后,目錄結(jié)構(gòu)如下
<?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!
|
*/
//get方法的第一個(gè)參數(shù)'/'代表網(wǎng)站的入口地址,第二個(gè)參數(shù)訪問的是app/http/controllers/home下的indexCon控制器的index方法
Route::get('/', 'home\indexCon@index');
//get方法的第一個(gè)參數(shù)'/home/article'訪問地址為backstage.com/goods,第二個(gè)參數(shù)訪問的是app/http/controllers/home下的goodsCon控制器的index方法
Route::get('/goods','home\goodsCon@index');
//添加商品
Route::get('/goods/add','home\goodsCon@addGoods');
//刪除商品
Route::get('/goods/del','home\goodsCon@delGoods');
//更新商品
Route::get('/goods/update','home\goodsCon@updateGoods');
<?php
namespace App\Http\Controllers\home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class goodsCon extends Controller
{
//
public function index()
{
return $this->showGoods();
}
//顯示商品
public function showGoods()
{
$res = DB::select('select * from tb_goods');
$data = [];
foreach($res as $key=>$val):
$data[] = (array)$val;
endforeach;
//注意goods.blade.php文件接收的是$result數(shù)組中的鍵名為res的這個(gè)變量的值
$result['res']=$data;
return view('home/goods',$result);
}
//添加商品
public function addGoods()
{
$date = date('Y-m-d');
$res = DB::insert("insert into `tb_goods` (`name`,`price`,`unit`,`sdate`) values ('黃桃','10','斤','$date') ");
echo $res;
}
//刪除商品
public function delGoods()
{
$res = DB::delete('delete from tb_goods where id>22');
echo $res;
}
//更新商品
public function updateGoods()
{
$res = DB::update('update tb_goods set name="西瓜" where id="22"');
echo $res;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>前臺(tái)商品展示頁</title>
<link rel="stylesheet" href="layui/css/layui.css">
<script src="layui/layui.js"></script>
</head>
<body>
<h2>商品展示頁</h2>
<table class="layui-table">
<thead>
<tr>
<td>商品編號(hào)</td>
<td>商品名稱</td>
<td>商品單價(jià)</td>
<td>商品單位</td>
<td>發(fā)布日期</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<?php
/* $res 這個(gè)變量的值是來自showCon控制器中 */
foreach($res as $goods):
?>
<tr>
<td><?php echo $goods['id'];?></td>
<td><?php echo $goods['name'];?></td>
<td><?php echo $goods['price'];?></td>
<td><?php echo $goods['unit'];?></td>
<td><?php echo $goods['sdate'];?></td>
<td><button class="layui-btn">修改</button><button class="layui-btn">刪除</button></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
</body>
</html>
微信掃碼
關(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)