laravel5.4 管理員的增刪改查
阿新 • • 發佈:2018-12-17
路由
/*管理員*/ Route::group(array('prefix'=>'user'),function(){ // 管理員顯示 Route::any('/lst', '\App\Http\Controllers\Admin\[email protected]'); //管理員新增頁面 Route::any('/add', '\App\Http\Controllers\Admin\[email protected]'); //管理員新增邏輯 Route::any('/store', '\App\Http\Controllers\Admin\[email protected]'); //管理員修改頁面 Route::any('/edit/{post}', '\App\Http\Controllers\Admin\[email protected]'); //管理員修改邏輯 Route::any('/update/{post}', '\App\Http\Controllers\Admin\[email protected]'); //管理員刪除 Route::any('/delete/{post}', '\App\Http\Controllers\Admin\[email protected]'); });
控制器
<?php namespace App\Http\Controllers\Admin; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; use App\Http\Model\Users; use App\Http\Controllers\Controller; class UserController extends CommonController { //any.admin/user 全部管理員列表 public function lst() { //通過模型查詢資料 $data = Users::orderBy('id','desc')->paginate(3);;//這裡的Users 是通過php artisan 建立的模型,而不是本 //控制器 UserController,使用模型類,必須引入改模型,即 use App\Users; return view('admin.user.lst',compact('data')); } //any.admin/user/add 新增管理員頁面 public function add() { return view('admin.user.add',compact('data')); } //新增管理員邏輯 public function store() { $username=\Request::input('username'); $password=md5(\Request::input('password')); $updated_at=time(); $time=time(); $res=DB::table('users')->insert(['username'=>$username,'password'=>MD5($password),'updated_at'=>$updated_at,'time'=>$time]); if($res){ return redirect('admin/user/lst'); }else{ return back()->with('errors','管理員新增失敗,請稍後重試!'); } } // 修改管理員頁面 public function edit($id) { $field = Users::find($id); return view('admin.user.edit',compact('data','field')); } //修改管理員邏輯 public function update($id) { $username=\Request::input('username'); $password=md5(\Request::input('password')); $data=[ 'username'=>$username, 'password'=>$password, 'updated_at'=>time(), 'time'=>time() ]; $re = Users::where('id',$id)->update($data); if($re){ return redirect('admin/user/lst'); }else{ return back()->with('errors','修改管理員失敗,請稍後重試!'); } } // 刪除 public function delete($id) { $re = Users::where('id',$id)->delete(); if($re){ return redirect('admin/user/lst'); }else{ return back()->with('errors','刪除管理員失敗,請稍後重試!'); } } }
檢視
增
<form class="form-horizontal" role="form" action="{{url('admin/user/store')}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <div class="form-group"> <label for="username" class="col-sm-2 control-label no-padding-right">管理員名</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="username" required="" type="text"> </div> <p class="help-block col-sm-4 red">* 必填</p> </div> <div class="form-group"> <label for="group_id" class="col-sm-2 control-label no-padding-right">管理員密碼</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="password" required="" type="text"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">儲存資訊</button> </div> </div> </form>
改
<form class="form-horizontal" role="form" action="{{url('admin/user/update/'.$field->id)}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <div class="form-group"> <label for="username" class="col-sm-2 control-label no-padding-right">管理員名</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="username" required="" type="text" value="{{$field->username}}"> </div> <p class="help-block col-sm-4 red">* 必填</p> </div> <div class="form-group"> <label for="group_id" class="col-sm-2 control-label no-padding-right">管理員密碼</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="password" required="" type="password" value="{{$field->password}}"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">儲存資訊</button> </div> </div> </form>
查
<div class="row"> <div class="col-lg-12 col-sm-12 col-xs-12"> <div class="widget"> <div class="widget-body"> <div class="flip-scroll"> <table class="table table-bordered table-hover"> <thead class=""> <tr> <th class="text-center">ID</th> <th class="text-center">使用者名稱稱</th> <th class="text-center">操作</th> <th class="text-center">操作日期</th> </tr> </thead> <tbody> @foreach ($data as $v) <tr> <td align="center">{{$v->id }} </td> <td align="center">{{$v->username}}</td> <td align="center"> <a href="{{url('admin/user/edit/'.$v->id.'')}}" class="btn btn-primary btn-sm shiny"> <i class="fa fa-edit"></i> 編輯 </a> <a href="{{url('admin/user/delete/'.$v->id.'')}}" class="btn btn-primary btn-sm shiny"> <i class="fa fa-edit"></i> 刪除 </a> </td> <td align="center">{{date('Y-m-d H:i:s',$v->time)}}</td> </tr> @endforeach </tbody> </table> </div> <div> </div> {{ $data->links() }} </div> </div> </div> </div>