1.index.blade.php
添加页面
<form action="add" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td>头像:</td>
<td><input type="file" name="img"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="添 加"/></td>
</tr>
</table>
</form>
2.show.blade.php
列表展示页面
<center>
<div id="div1">
<input type="text" name="search" value="{{$search}}"/><button onclick="page(<?php echo $page?>)">搜索</button>
<table border="1">
<tr>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
<td>添加时间</td>
<td>头像</td>
<td>操作</td>
</tr>
@foreach($res as $key=>$val)
<tr>
<td>{{$val->id}}</td>
<td>{{$val->username}}</td>
<td>{{$val->pwd}}</td>
<td>{{$val->add_time}}</td>
<td><img src="{{$val->img}}" width="80px" height="80px" alt="头像"></td>
<td><a href="{{url('del')}}?id={{$val->id}}">删除</a>||<a href="{{url('upd')}}?id={{$val->id}}">修改</a></td>
</tr>
@endforeach
</table>
当前页<?php echo $page?>共<?php echo $sum_page?>
<a href="javascript:void (0)" onclick="page(1)">首页</a>
<a href="javascript:void (0)" onclick="page(<?php echo $last?>)">上一页</a>
<a href="javascript:void (0)" onclick="page(<?php echo $next?>)">下一页</a>
<a href="javascript:void (0)" onclick="page(<?php echo $sum_page?>)">尾页</a>
</div>
</center>
<script>
//分页
function page(page){
var search=document.getElementsByName('search')[0].value;
//创建xhr对象
var xhr=new XMLHttpRequest();
//打开请求
xhr.open('get','show?page='+page+'&search='+search);
//发送请求
xhr.send();
// 创建回调函数
xhr.onreadystatechange=function(){
//判断ajax的状态和网络状态是否等于4 和200
if(xhr.readyState==4&xhr.status==200){
document.getElementById('div1').innerHTML=xhr.responseText;
}
}
}
</script>
3.update.blade.php
修改页面
<form action="{{url('update')}}" method="get">
<table>
<input type="hidden" name="id" value="{{$arr->id}}"/>
<tr>
<td>用户名</td>
<td><input type="text" name="username" value="{{$arr->username}}"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" value="{{$arr->pwd}}"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="修改"/></td>
</tr>
</table>
</form>
4. UserController.php
控制器页面
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller{
public function index()
{
return view('user/index');
}
//执行添加
public function add(Request $request){
$username=$request->input('username');
$pwd=$request->input('pwd');
if($request->hasFile('img')&&$request->file('img')->isValid()){
//接收表单传过来的文件
$file=$request->file('img');
//设置允许上传的文件后缀
$allowed_extensions = ["png", "jpg", "gif","jpeg"];
//获取上传文件的大小 getClientSize() laravel8
$filesize=$file->getSize();
if($filesize>88888888){
dd('文件过大');
}
//获取上传文件的后缀(如abc.png,获取到的为png)
//in_array()检测一个值是否存在数组中
if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
dd('只能上传png,jpg和gif格式的图片.');
}else{
$destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
//获取文件后缀
$extension = $file->getClientOriginalExtension();
//文件名称由时间戳.1-1000随机数 然后再加密组成 f6c16a5e3f1a0fa40edc788327448a9b.jpg
$fileName=md5(time().rand(1,1000)).'.'.$extension;
//将上传的文件移动至指定的目录下
$file->move($destinationPath,$fileName);
//加入数据库的文件 保存路径/文件名 /storage/uploads/b170a2955f1ac97a84b8acd89f0cfb33.jpg
$filePath = $destinationPath.$fileName;
// dd($filePath);
}
}else{
dd('图片上传失败请重试.');
}
//执行添加
$res= DB::table('user')->insert([
'username' =>$username,
'pwd' =>$pwd,
'add_time'=>date("Y-m-d h:i:s",time()),
'img' =>$filePath,
]);
if($res){
return Redirect('show');
}
else{
die('添加失败');
}
}
//分页查询开始
public function show(){
$search = isset($_GET['search'])?$_GET['search']:"";//判断是否接过搜索值
if($search)
{
$arr = DB::select("select * from user where username like '%$search%' or pwd like '%$search%'");//带搜索条件查询
}else{
$arr = DB::table('user')->get();//不带搜索条件查询
}
//总条数
$count = count($arr);
//每页条数
$mys = 3 ;
//计算总页数
$zys = ceil($count/$mys);
//获取当前页
$page = isset($_GET['page'])?$_GET['page']:1;
//偏移量
$pyl = ($page-1)*$mys;
if($search)
{
$arr = DB::select("select * from user where username like '%$search%' or pwd like '%$search%' limit $pyl,$mys");//带搜索分页条件查询
/*foreach($arr as $key => $value) {
$arr[$key]['username'] = str_replace($search, "<font color='red'>$search</font>", $value['username']);
}*/
}else{
$arr = DB::table('user')->skip($pyl)->take($mys)->get();//带分页条件查询
}
//上一页
$last = $page-1<1?1:$page-1;
//下一页
$next = $page+1>$zys?$zys:$page+1;
return view('user/show',['res'=>$arr,'search'=>$search,'last'=>$last,'next'=>$next,'sum_page'=>$zys,'page'=>$page]);
}
//分页查询结束
//删除
public function del(Request $request){
$id=$request->input('id');
$res=DB::table('user')->where('id',$id)->delete();
if($res==1){
return redirect('show');
}
}
//修改页面
public function upd(Request $request){
$id=$request->input('id');
$arr=DB::table('user')->where('id',$id)->first();
return view('user/update',['arr'=>$arr]);
}
//执行修改
public function update(Request $request){
$id=$request->input('id');
$username=$request->input('username');
$pwd=$request->input('pwd');
$res=DB::table('user')->where('id',$id)->update(array('username'=>$username,'pwd'=>$pwd));
if($res==1){
return redirect('show');
}
}
}
?>
5.数据库表-user表
大功告成!感谢您的收看,老铁互关,点赞,一起加油
支持原创!!!