1. 程式人生 > >laravel 5.3 CURD

laravel 5.3 CURD

初學者的 CURD

控制器下的程式碼

路由設定

新增

  /*
    * 新增
    */
     public function insert(){
          input::all('name');
         $name=$_POST['name'];
         $sex=$_POST['sex'];
         $phone=$_POST['phone'];
         $res= DB::table('user')->insert([
             'name' =>$name,
             'sex' =>$sex,
             'phone' =>$phone,
         ]);
         if($res){
             return Redirect('info');
         }else{
             die('新增失敗');
         }

     }

分頁顯示

 public function info(){
         //顯示
        // $users = DB::table('user')->get();
         //顯示分頁
         $users = DB::table('user')->paginate(3);
         return View('User/info',['name'=>$users]);

     }

刪除

/*
     *刪除
     */
     public function delete($id){
         $users = DB::table('user')->where('id',$id)->delete();
         if($users){
             echo "刪除成功";
         }else{
             echo "刪除失敗";
         }
          return Redirect('info');
     }

修改

  /*
     *修改
     */
     //修改頁面
     public function upd($id){
         $arr=DB::table('user')->where('id',$id)->first();
         return view('user/upd',['arr'=>$arr]);
     }
     //執行修改
     public function update(){
          $id=$_POST['id'];
         $name=$_POST['name'];
         $sex=$_POST['sex'];
         $phone=$_POST['phone'];
         $res=DB::table('user')->where('id',$id)->update(array('name'=>$name,
             'sex'=>$sex,
             'phone'=>$phone));
         if($res){
             return Redirect('info');
         }else{
             echo "失敗";
         }
     }

html頁面

顯示頁面

 <div id="type_a">
        <a href="{{url('show')}}" >新增資訊</a>
    </div>
   <table border="1">
       <tr>
           <td>id</td>
           <td>姓名</td>
           <td>性別</td>
           <td>手機</td>
           <td>操作</td>
       </tr>
   @foreach ($name as $v)
       <tr>
           <td>{{$v->id}}</td>
           <td>{{$v->name}}</td>
           <td>{{$v->sex}}</td>
           <td>{{$v->phone}}</td>
           <td><a onclick="if(confirm('確認刪除嗎?') == false) return false;"
                 href="{{ url('user/delete', ['id' => $v->id]) }}">刪除</a>
              <a href="{{ url('user/upd', ['id' => $v->id]) }}">修改</a>
          </td>
       </tr>
   @endforeach

   </table>
<div id="pull_right">
    <div class="pull-right">
        {{ $name->render() }}
    </div>
</div>

因為laravel的分頁樣式太難看了所以自己改了樣式

<style type="text/css">
    #pull_right{
        text-align:center;
    }
    .pull-right {
        /*float: left!important;*/
    }
    .pagination {
        display: inline-block;
        padding-left: 0;
        margin: 20px 0;
        border-radius: 4px;
    }
    .pagination > li {
        display: inline;
    }
    .pagination > li > a,
    .pagination > li > span {
        position: relative;
        float: left;
        padding: 6px 12px;
        margin-left: -1px;
        line-height: 1.42857143;
        color: #428bca;
        text-decoration: none;
        background-color: #fff;
        border: 1px solid #ddd;
    }
    .pagination > li:first-child > a,
    .pagination > li:first-child > span {
        margin-left: 0;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
    }
    .pagination > li:last-child > a,
    .pagination > li:last-child > span {
        border-top-right-radius: 4px;
        border-bottom-right-radius: 4px;
    }
    .pagination > li > a:hover,
    .pagination > li > span:hover,
    .pagination > li > a:focus,
    .pagination > li > span:focus {
        color: #2a6496;
        background-color: #eee;
        border-color: #ddd;
    }
    .pagination > .active > a,
    .pagination > .active > span,
    .pagination > .active > a:hover,
    .pagination > .active > span:hover,
    .pagination > .active > a:focus,
    .pagination > .active > span:focus {
        z-index: 2;
        color: #fff;
        cursor: default;
        background-color: #428bca;
        border-color: #428bca;
    }
    .pagination > .disabled > span,
    .pagination > .disabled > span:hover,
    .pagination > .disabled > span:focus,
    .pagination > .disabled > a,
    .pagination > .disabled > a:hover,
    .pagination > .disabled > a:focus {
        color: #777;
        cursor: not-allowed;
        background-color: #fff;
        border-color: #ddd;
    }
    .clear{
        clear: both;
    }
</style>
<div id="pull_right">
    <div class="pull-right">
        {{ $name->render() }}
    </div>
</div>

效果如下