1. 程式人生 > >laravel 增刪改查 數據庫設置 路由設置

laravel 增刪改查 數據庫設置 路由設置

namespace ces sources prefix int word 大致 mes fix

技術分享
laravel 框架的路由設置:          url:   http://www.shanzezhao.com/laraverl/my_laravel/public/index.php/indexs
技術分享


laravel 框架的數據庫設置:config/database.php
技術分享
 1  ‘mysql‘ => [
2 ‘driver‘ => ‘mysql‘, 3 ‘host‘ => ‘localhost‘, 4 ‘port‘ => ‘3306‘, 5 ‘database‘ => ‘laravel‘, 6 ‘username‘ => ‘root‘, 7 ‘password‘ => ‘123456‘, 8 ‘unix_socket‘ => ‘‘, 9 ‘charset‘ => ‘utf8‘, 10 ‘collation‘ => ‘utf8_unicode_ci‘, 11 ‘prefix‘ => ‘‘, 12 ‘strict‘ => true, 13 ‘engine‘ => null, 14 ],
技術分享


laravel 框架的增刪改查::


1 <?php 2 3 4 namespace App\Http\Controllers; 5 6 use DB; 7 use App\Http\Controllers\Controller; 8 9 10 class IndexController extends Controller 11 { 12 13 //添加展示 14 public function index() 15 { 16 return view(‘index/index‘); 17 18 } 19 //添加 20 public function add() 21 { 22 23 $name=$_POST[‘uname‘]; 24 $pwd=$_POST[‘upwd‘]; 25 // print_r($pwd);die; 26 //設置數據表
      $re=DB::table(‘text1‘)->insert([‘uname‘=>$name,‘upwd‘=>MD5($pwd)]); 27 // print_r($re);die; 28 if($re) 29 { 30 return redirect(‘show‘); 31 } 32 } 33 //數據展示 34 public function show() 35 { 36 $re = DB::table(‘text1‘)->get(); 37 // print_r($re);die; 38 return view(‘index/show‘,[‘re‘=>$re]); 39 } 40 //刪除 41 public function deletes() 42 { 43 $id=$_GET[‘id‘]; 44 // print_r($id);die; 45 $re= DB::table(‘text1‘) 46 ->where(‘id‘,$id) 47 ->delete(); 48 if($re) 49 { 50 return redirect(‘show‘); 51 } 52 53 } 54 //修改頁面 55 public function updates() 56 { 57 $id=$_GET[‘id‘]; 58 //print_r($id);die; 59 $re = DB::table(‘text1‘)->where(‘id‘,$id)->first(); 60 // print_r($re);die; 61 return view(‘index/upd‘,[‘re‘=>$re]); 62 63 64 } 65 //修改 66 public function upd() 67 { 68 $name=$_POST[‘uname‘]; 69 $pwd=$_POST[‘upwd‘]; 70 $id=$_POST[‘id‘]; 71 $arr=array(‘id‘=>$id,‘uname‘=>$name,‘upwd‘=>$pwd); 72 $re=DB::table(‘text1‘) 73 ->where(‘id‘,‘=‘,$id ) 74 ->update($arr); 75 if($re) 76 { 77 return redirect(‘show‘); 78 } 79 80 81 } 82 83 }
技術分享

表單和以前學的框架大致還是沒有什麽區別的

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
技術分享

laravel 增刪改查 數據庫設置 路由設置