view视图中的代码
<body>
<form action="{{url('fuxi_yi')}}" method="post" enctype="multipart/form-data" >
{{csrf_field()}}
用户名:<input type="text" name="user[]"><br>
密码:<input type="text" name="pwd[]"><br>
头像:<input type="file" name="img[]"><br>
<span class="zheli"></span><br>
<input type="submit">
<input type="button" name="tj" onclick="fun()" value="添加">
</form>
</body>
</html>
<script src="{{asset('assets')}}/js1/jquery.min.js"></script>
<script>
function fun() {
var tr = ("<input type='text' name='user[]'><br><input type='text' name='pwd[]'><br> <input type='file' name='img[]'><br>");
$('.zheli').append(tr+'<br>');
}
</script>
controller控制器中的代码
public function zhuce(){
// echo 123;die;
$s = new Fuxi();
$s->zhuce();
$list = DB::table('fuxi')->select()->get();
return view('Fuxi/Fuxi_xianshi',['list'=>$list]);
}
model模型中的代码
//这个方法完成批量添加信息 以及批量添加文件
public function zhuce(){
//获取前台传来的所有数据
$list = request()->all();
$img = request()->file('img');
//用来计数看看传来几组数据
$count = count($list['pwd']);
//申请一个空数据 用来存放所有的图片
$str = [];
for($i=0;$i<$count;$i++){
/* $_POST['user'][$i]*/
//这里进行循环 有几组数据 就自动组成几个随机文件名称
array_push($str,date('YmdHis').rand(100,9999).'.'.'png');
$img[$i]->move('./image/',$str[$i]);
//这里存放进一个数组 然后进行插入
$data[] = [
'user'=>$_POST['user'][$i],
'pwd'=>$_POST['pwd'][$i],
'img'=>$str[$i]
];
//有几条数据 向数据库插入几次 以完成批量添加
DB::table('fuxi')->insert($data);
}
// $img = file_get_contents('./image/201811181126171418.png');
}
路由
Route::any('fuxi_yi',"Fuxicontroller\Fuxicontroller@zhuce");
显示数据 显示添加的所有内容view视图
<table>
<tr>
<th>用户名</th>
<th>密码</th>
<th height="50px" width="50px">头像</th>
<th >操作</th>
</tr>
@foreach($list as $v)
<tr>
<td>{{$v->user}}</td>
<td>{{$v->pwd}}</td>
<td><img src="{{asset('image')}}/{{$v->img}}" height="50px" width="50px"></td>
<td><a href="{{url('')}}">修改</a>丨<a href="{{url('')}}">删除</a></td>
</tr>
@endforeach
</table>