lavarel框架增删改查

王佐
2023-12-01
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Hello;
use App\Http\Controllers\Controller;
use DB;
class HelloController extends Controller{
    public function Add(){
        return view('xiaoer');
    }
    /*
     * 添加
     */
    public function abb(Request $request){
        $uname = $request->input('uname');
        //echo $uname;die;
        $pwd = $request->input('pwd');
        //echo $pwd;die;
        $photo = $request->input('photo');
        $arr = DB::table('inpu')->insert(array('uname'=>$uname,'pwd'=>$pwd,'photo'=>$photo));
        //echo $arr;
        if($arr==1){
            return redirect('acc');
        }
    }
    /*
     * 展示列表
     */
    public function acc(){
        $arr = DB::table('inpu')->get();//原来的
        //return view('user/show', ['arr' => $arr]);//原来的
        //分页开始
        $count=count($arr);//获取总条数
        $page_num=3;//每页显示条数
        $sum_page=ceil($count/$page_num);//总页数
        $page=isset($_GET['page'])?$_GET['page']:1;//当前页
        $page_limit=($page-1)*$page_num;//偏移量
        //$res= DB::table('user')->limit($page_limit,$page_num)->get();//偏移量之后的查询
        $res = DB::table('inpu')->skip("$page_limit")->take("$page_num")->get();
        //print_r($res);die;
        $last=$page-1<1?1:$page-1;//获取上一页
        $next=$page+1>$sum_page?$sum_page:$page+1;//获取下一页
        return view('form',['arr' => $res,'last'=>$last,'next'=>$next,'page'=>$page,'sum_page'=>$sum_page]);
        //分页结束

    }
    /*
     * 删除
     */
    public function det(Request $request){
        $id=$request->input('id');
        //print_r($id);die;
        $arr=DB::table('inpu')->where('id',$id)->delete();
        if($arr==1){
            //echo 1;die;
            return redirect('acc');
        }
    }
    /*
     * 查询修改的语句
     */
    public function up(Request $request){
        $id=$request->input('id');
        //print_r($id);die;
        $res=DB::table('inpu')->where('id',$id)->first();
        return view('azz',['arr'=>$res]);
    }
    /*
     * 执行修改
     */
    public function update(Request $request){
        $id = $request->input('id');
        $uname = $request->input('uname');
        $pwd = $request->input('pwd');
        $photo = $request->input('photo');
        $arr = DB::table('inpu')->where('id',$id)->update(array('uname'=>$uname,'pwd'=>$pwd,'photo'=>$photo));
        //print_r($arr);die;
        if($arr==1){
            return redirect('acc');
        }
    }

}

2:创建展示页面:form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
</head>
<body>
<center>
    名称:<input type="text" name="uname">
    <input type="button" value="搜索" οnclick="search()"><br />
        <div id="div1">
        <table border="1">
                <tr>
                    <td>ID</td>
                    <td>用户名</td>
                    <td>密码</td>
                    <td>手机号</td>
                    <td>操作</td>
                </tr>
                @foreach($arr as $k=>$v)
                    <tr>
                        <td>{{$v->id}}</td>
                        <td>{{$v->uname}}</td>
                        <td>{{$v->pwd}}</td>
                        <td>{{$v->photo}}</td>
                        <td><a href="{{url('det')}}?id={{$v->id}}">删除</a><a href="{{url('up')}}?id={{$v->id}}">修改</a></td>
                    </tr>
                @endforeach
        </table>
        当前页<?php echo $page?>共<?php echo $sum_page?>
        <a href="javascript:void (0)" οnclick="page(1)">首页</a>
        <a href="javascript:void (0)" οnclick="page(<?php echo $last?>)">上一页</a>
        <a href="javascript:void (0)" οnclick="page(<?php echo $next?>)">下一页</a>
        <a href="javascript:void (0)" οnclick="page(<?php echo $sum_page?>)">尾页</a>
        </div>

    </center>
</body>
</html>
<script>
    function page(page){
        //var search=document.getElementsByName('search')[0].value;
        var ajax=new XMLHttpRequest();
        ajax.open('get','{{url('acc')}}?page='+page);
        ajax.send();
        ajax.onreadystatechange=function(){
            if(ajax.readyState==4&ajax.status==200){
                document.getElementById('div1').innerHTML=ajax.responseText;
            }

        }
    }


</script>


 类似资料: