laravel5.4 欄目的增刪改查
阿新 • • 發佈:2018-12-17
路由
/*欄目*/ Route::group(array('prefix'=>'cate'),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\Cates; use App\Http\Controllers\Controller; class CateController extends CommonController { public function lst() { //通過模型查詢資料 $data = Cates::orderBy('id','desc')->paginate(3); return view('admin.cate.lst',compact('data')); } //any.admin/user/add 新增欄目頁面 public function add() { return view('admin.cate.add',compact('data')); } //新增欄目邏輯 public function store() { $catename=\Request::input('catename'); $updated_at=time(); $time=time(); $res=DB::table('cates')->insert(['catename'=>$catename,'updated_at'=>$updated_at,'time'=>$time]); if($res){ return redirect('admin/cate/lst'); }else{ return back()->with('errors','欄目新增失敗,請稍後重試!'); } } // 修改欄目頁面 public function edit($id) { $field = Cate::find($id); return view('admin.cate.edit',compact('data','field')); } //修改欄目邏輯 public function update($id) { $catename=\Request::input('catename'); $data=[ 'catename'=>$catename, 'time'=>time(), 'updated_at'=>time() ]; $re = Cate::where('id',$id)->update($data); if($re){ return redirect('admin/cate/lst'); }else{ return back()->with('errors','修改欄目失敗,請稍後重試!'); } } // 刪除 public function delete($id) { $re = Cate::where('id',$id)->delete(); if($re){ return redirect('admin/cate/lst'); }else{ return back()->with('errors','刪除欄目失敗,請稍後重試!'); } } } 檢視
增
<form class="form-horizontal" role="form" action="{{url('admin/cate/store')}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">欄目名</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="catename" 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/cate/update/'.$field->id)}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">欄目名</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="catename" required="" type="text" value="{{$field->catename}}"> </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->catename}}</td> <td align="center"> <a href="{{url('admin/cate/edit/'.$v->id.'')}}" class="btn btn-primary btn-sm shiny"> <i class="fa fa-edit"></i> 編輯 </a> <a href="{{url('admin/cate/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>