一: 创建添加文件页面的路由
Route::get('create',[MenuController::class,'create'])->name('create');
二 : 搭建添加页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加</title>
</head>
<body>
<form action="{{route('store')}}" method="post" enctype="multipart/form-data">
图片 : <input type="file" name="image"> <br>
<input type="submit" value="添加">
</form>
</body>
</html>
三 : 创建处理添加信息的路由
Route::post('store',[MenuController::class,'store'])->name('store');
四 : 在控制器内创建相应的方法
public function store(Request $request)
{
//接文件
$file = $request->file('image')->getPathname();
//拼接文件名称
$fileName['image'] = md5($file) . '.png';
//执行添加
$create = Menu::create($fileNmae['image']);
//判断是否添加成功
if ($create){
//添加成功
$request->image->move(public_path('images'), $fileName);
//移动到public目录
return redirect(route('index'))->with('添加成功');
}else{
//添加失败
return back()->withError(['error' => '添加失败'])->withInput();
}
}
五 : 创建列表的路由
Route::get('index',[MenuController::class,'index'])->name('index');
六 : 列表的方法
public function index()
{
//分页查询全部数据
$data = Mneu::paginate(5);
//返回页面
return view('index',compact('data'));
}
七 : 页面的展示
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<title>列表</title>
</head>
<body>
<table class="table">
<tr>
<td>菜单图片</td>
</tr>
@foreach($data as $v)
<tr>
<td>
<img src="images/{{$v->image}}" alt="">
</td>
</tr>
@endforeach
</table>
</body>
</html>