zoukankan      html  css  js  c++  java
  • laravel——基础增删改查

    一、控制器代码

    <?php
    namespace AppHttpControllers;
    use IlluminateSupportFacadesDB;
    
    class CurdController extends Controller
    {
        public function index()
        {
            return view('curd/add');
        }
        public function add()
        {
            $data = $_POST;
            $res = DB::table('customer')->insert([
                'bh'=>$data['bh'],
                'uname'=>$data['uname'],
                'charge'=>$data['charge'],
                'tel'=>$data['tel'],
                'content'=>$data['content']
            ]);
            if($res)
            {
                return redirect('curd/show');
            }
        }
        public function show()
        {
            $data = DB::table('customer')->get();
            return view('curd/show',['data'=>$data]);
        }
        public function del()
        {
            $id = $_GET['id'];
            $res = DB::table('customer')->where('id',$id)->delete();
            if($res)
            {
                return redirect('curd/show');
            }
        }
        public function up()
        {
            $id = $_GET['id'];
            $data = DB::table('customer')->where('id',$id)->first();
            return view('curd/upda',['data'=>$data]);
        }
        public function upda()
        {
            $data = $_POST;
            $id = $_POST['id'];
            unset($data['_token']);
            $res = DB::table('customer')->where('id','=',$id)->update($data);
            if($res)
            {
                return redirect('curd/show');
            }
        }
    }

    二、视图代码

    (1)添加页面

    <!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="add" method="post">
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <table border="1">
            <tr>
                <td>编号</td>
                <td><input type="text" name="bh"></td>
            </tr>
            <tr>
                <td>客户名称</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>负责人</td>
                <td><input type="text" name="charge"></td>
            </tr>
            <tr>
                <td>公司电话</td>
                <td><input type="text" name="tel"></td>
            </tr>
            <tr>
                <td>描述</td>
                <td><input type="text" name="content"></td>
            </tr>
            <tr>
                <td><input type="submit" value="提交"></td>
                <td></td>
            </tr>
        </table>
    </form>
    </body>
    </html>

    (2)展示页面

    <!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>
    <table border="1">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>负责人</th>
            <th>电话</th>
            <th>描述</th>
            <th>操作</th>
        </tr>
        <?php
            foreach ($data as $k=>$v)
            {
        ?>
               <tr>
                   <td><?php echo $v->bh; ?></td>
                   <td><?php echo $v->uname; ?></td>
                   <td><?php echo $v->charge; ?></td>
                   <td><?php echo $v->tel; ?></td>
                   <td><?php echo $v->content; ?></td>
                   <td>
                       <a href="del?id=<?php echo $v->id; ?>">删除</a> |
                       <a href="up?id=<?php echo $v->id; ?>">修改</a>
                   </td>
               </tr>
         <?php  } ?>
    
    </table>
    </body>
    </html>

    (3)修改的默认页面

    <form action="upda" method="post">
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <input type="hidden" name="id" value="<?php echo $data->id; ?>">
        <table border="1">
            <tr>
                <td>编号</td>
                <td><input type="text" name="bh" value="<?php echo $data->bh; ?>"></td>
            </tr>
            <tr>
                <td>客户名称</td>
                <td><input type="text" name="uname" value="<?php echo $data->uname; ?>"></td>
            </tr>
            <tr>
                <td>负责人</td>
                <td><input type="text" name="charge" value="<?php echo $data->charge; ?>"></td>
            </tr>
            <tr>
                <td>公司电话</td>
                <td><input type="text" name="tel" value="<?php echo $data->tel; ?>"></td>
            </tr>
            <tr>
                <td>描述</td>
                <td><input type="text" name="content" value="<?php echo $data->content; ?>"></td>
            </tr>
            <tr>
                <td><input type="submit" value="修改"></td>
                <td></td>
            </tr>
        </table>
    </form>
  • 相关阅读:
    vue 中引用echarts 初始化init undefind问题(Cannot read property ‘init‘ of undefined)
    粘性定位(position:sticky)实现固定表格表头、固定列
    js替换字符串中的空格,换行符 或 替换成<br>
    一个完整的大作业
    数据结构化与保存
    爬取所有校园新闻
    用requests库和BeautifulSoup4库爬取新闻列表
    中文词频统计及词云制作
    组合数据类型练习,英文词频统计实例
    字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
  • 原文地址:https://www.cnblogs.com/wxy0126/p/10698485.html
Copyright © 2011-2022 走看看