laravel 增删改查 数据库设置 路由设置

时间:2022-07-06 10:11:39
laravel 框架的路由设置:          url:   http://www.shanzezhao.com/laraverl/my_laravel/public/index.php/indexs
laravel 增删改查 数据库设置 路由设置
laravel 框架的数据库设置:config/database.php
  'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'laravel',
'username' => 'root',
'password' => '123456',
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

laravel 框架的增删改查::
 1 <?php

 namespace App\Http\Controllers;

 use DB;
use App\Http\Controllers\Controller; class IndexController extends Controller
{ //添加展示
public function index()
{
return view('index/index'); }
//添加
public function add()
{ $name=$_POST['uname'];
$pwd=$_POST['upwd'];
// print_r($pwd);die;
//设置数据表
      $re=DB::table('text1')->insert(['uname'=>$name,'upwd'=>MD5($pwd)]);
// print_r($re);die;
if($re)
{
return redirect('show');
}
}
//数据展示
public function show()
{
$re = DB::table('text1')->get();
// print_r($re);die;
return view('index/show',['re'=>$re]);
}
//删除
public function deletes()
{
$id=$_GET['id'];
// print_r($id);die;
$re= DB::table('text1')
->where('id',$id)
->delete();
if($re)
{
return redirect('show');
} }
//修改页面
public function updates()
{
$id=$_GET['id'];
//print_r($id);die;
$re = DB::table('text1')->where('id',$id)->first();
// print_r($re);die;
return view('index/upd',['re'=>$re]); }
//修改
public function upd()
{
$name=$_POST['uname'];
$pwd=$_POST['upwd'];
$id=$_POST['id'];
$arr=array('id'=>$id,'uname'=>$name,'upwd'=>$pwd);
$re=DB::table('text1')
->where('id','=',$id )
->update($arr);
if($re)
{
return redirect('show');
} } }

表单和以前学的框架大致还是没有什么区别的

resources/views/index/index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="add" method="post" >
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="upwd"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>

show.php

<!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->uname; ?></td>
<td><?php echo $val->upwd; ?></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>
</center>
</body>
</html>

upd.php

<!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="uname" value="<?php echo $re->uname ?>"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="upwd" value="<?php echo $re->upwd ?>"></td>
</tr>
<tr>
<td><input type="submit" value="修改"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html