1:首先先定义路由,一个方法写一个路由
Route::get('index','IndexController@index');//首页 Route::post('add','IndexController@add');//添加 Route::any('show','IndexController@show');//展示 Route::any('del/{id}','IndexController@del');//删除 Route::any('sa/{id}','IndexController@sa');//查询单条数据 Route::post('upda','IndexController@upda');//修改2:控制器
<?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; //设置input接值方式 use Illuminate\Support\Facades\Input; use DB; class IndexController extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests; //展示页面 public function index(){ return view('index/index'); } /** *添加页面 */ public function add(){ $title=input::get('title'); //echo $title; $content=input::get('content'); //echo $content; $arr= DB::table('cmss')->insert(array('title'=>$title,'content'=>$content)); if ($arr){ return redirect()->action('IndexController@show'); } } /** * 展示页面 */ public function show(){ $arr=DB::table('cmss')->get(); //var_dump($arr);die; return view('show',['arr'=>$arr]); } /** * 删除 */ public function del($id){ //echo $id; $arr=DB::table('cmss')->where('id','=',$id)->delete(); if ($arr){ return redirect()->action('IndexController@show'); } } /** * 单条查询展示 */ public function sa($id){ //echo $id; $data=DB::table('cmss')->where('id',$id)->first(); //var_dump($data); return view('sa/sa',['data'=>$data]); } /** * 修改 */ public function upda(){ $arr=input::get(); //var_dump($arr); $data=DB::table('cmss')->where('id',$arr['id'])->update($arr); if($data){ return redirect()->action('IndexController@show'); } }3.视图层:
(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>Document</title> </head> <body> <center> <form action="{{(URL('add'))}}" method="post"> <table> <tr> <td>标题</td> <td><input type="text" name="title"></td> </tr> <tr> <td>内容</td> <td><input type="text" name="content"></td> </tr> <tr> <td></td> <td><input type="submit" value="sub"></td> </tr> </table> </form> </center> </body> </html>(2)展示
<table> <tr> <td>ID</td> <td>标题</td> <td>内容</td> <td>操作</td> </tr> <?php foreach ($arr as $k=>$v ): ?> <tr> <td><?=$v['id']?></td> <td><?=$v['title']?></td> <td><?=$v['content']?></td> <td><a href="del/{{$v['id']}}">删除</a>|<a href="sa/{{$v['id']}}">修改</a></td> </tr> <?php endforeach; ?> </table>(3)修改
<!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>Document</title> </head> <body> <center> <form action="{{URL('upda')}}" method="post"> <table> <tr> <td>标题</td> <td><input type="text" value="<?php echo $data['title']?>" name="title"></td> </tr> <tr> <td>内容</td> <td><input type="text" value="<?php echo $data['content']?>" name="content"></td> </tr> <tr> <td><input type="hidden" name="id" value="<?php echo $data['id']?>"></td> <td><input type="submit" value="修改"></td> </tr> </table> </form> </center> </body> </html>