laravel里面路有时很厉害的,首先要找见路由的地方
然后是控制器
接下来就是视图
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::any('index' ,'IndexController@index');
Route::post('add','IndexController@add');
Route::any('show','IndexController@show');
Route::any('deletes','IndexController@deletes');
Route::any('updates','IndexController@updates');
Route::any('upd','IndexController@upd');
然后是控制器
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/11/27 0027
* Time: 19:36
*/
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class IndexController extends BaseController
{
public function index()
{
return view('index/index');
}
public function add()
{
$name=$_POST['name'];
$pwd=$_POST['pwd'];
// print_r($pwd);die;
$re=DB::table('user')->insert(['name'=>$name,'pwd'=>$pwd]);
// print_r($re);die;
if($re)
{
return redirect('show');
}
}
public function show()
{
$re = DB::table('user')->get();
// print_r($re);die;
return view('index/show',['re'=>$re]);
}
public function deletes()
{
$id=$_GET['id'];
// print_r($id);die;
$re= DB::table('user')
->where('id',$id)
->delete();
if($re)
{
return redirect('show');
}
}
public function updates()
{
$id=$_GET['id'];
//print_r($id);die;
$re = DB::table('user')->where('id',$id)->first();
// print_r($re);die;
return view('index/upd',['re'=>$re]);
}
public function upd()
{
$name=$_POST['name'];
$pwd=$_POST['pwd'];
$id=$_POST['id'];
$arr=array('id'=>$id,'name'=>$name,'pwd'=>$pwd);
$re=DB::table('user')
->where('id','=',$id )
->update($arr);
if($re)
{
return redirect('show');
}
}
}
接下来就是视图
index视图
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="add" method="post">
<table align="center">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<tr>
<td>name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>pwd</td>
<td><input type="password" name="pwd" id=""/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="add"/></td>
</tr>
</table>
</form>
</body>
</html>
show视图<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<table>
<tr>
<td>姓名</td>
<td>加密密码</td>
<td>设置</td>
</tr>
<?php foreach ($re as $key => $val): ?>
<tr>
<td><?php echo $val->name; ?></td>
<td><?php echo $val->pwd; ?></td>
<td>
<a href="deletes?id=<?php echo $val->id ?>">删除</a>
<a href="updates?id=<?php echo $val->id ?>">修改</a>
</td>
</tr>
<?php endforeach ?>
</table>
<a href="add">添加</a>
</center>
</body>
</html>
upd视图<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="upd" method="post" >
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="hidden" name="id" value="<?php echo $re->id ?>">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" value="<?php echo $re->name ?>"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="pwd" value="<?php echo $re->pwd ?>"></td>
</tr>
<tr>
<td><input type="submit" value="修改"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
当然,这些所有的前提是你的数据库要配置好,在config下面的database.php中进行配置'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'demo'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],