laravel——基础增删改查

一、控制器代码

<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;

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>

猜你喜欢

转载自www.cnblogs.com/wxy0126/p/10698485.html