后盾网lavarel视频项目---lavarel使用模型进行增删改查操作
生活随笔
收集整理的這篇文章主要介紹了
后盾网lavarel视频项目---lavarel使用模型进行增删改查操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
后盾網lavarel視頻項目---lavarel使用模型進行增刪改查操作
一、總結
一句話總結:
使用模型操作常用方法
查一條:$model=Tag::find($id);
刪一條:Tag::destroy($id);
查全部:$data=Tag::get();
增加:$model->create($request->all());
?
1、資源路由器操作處理的動作、URL、行為、路由名稱?
看手冊嘍,修改和增加用的是兩個,一個是get顯示界面,一個是處理邏輯
| GET | /photos | index | photos.index |
| GET | /photos/create | create | photos.create |
| POST | /photos | store | photos.store |
| GET | /photos/{photo} | show | photos.show |
| GET | /photos/{photo}/edit | edit | photos.edit |
| PUT/PATCH | /photos/{photo} | update | photos.update |
| DELETE | /photos/{photo} | destroy | photos.destroy |
?
2、更新操作中,需要表單傳遞PUT請求,如何做?
偽造表單方法:@method('PUT')
<form action="/foo/bar" method="POST">@method('PUT') </form>?
?
3、解決剔除token字段存數據庫的問題?
模型中定義$guarded為空數組:protected $guarded=[];
?
4、jquery自動傳遞csrf的token字段?
在頁頭創建csrf-token的meta標簽,在頁尾用ajaxSetup設置token的值
X-CSRF-TOKEN 除了檢查 POST 參數中的 CSRF 令牌外, VerifyCsrfToken 中間件還會檢查 X-CSRF-TOKEN 請求頭。你應該將令牌保存在 HTML meta 標簽中,如下:<meta name="csrf-token" content="{{ csrf_token() }}"> 然后,一旦你創建了 meta 標簽,就可以指示像 jQuery 這樣的庫自動將令牌添加到所有請求的頭信息中。還可以為基于 AJAX 的應用提供簡單、方便的 CSRF 保護。如下:$.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); {tip} 默認情況下,resources/assets/js/bootstrap.js 文件會用 Axios HTTP 函數庫注冊 csrf-token meta 標簽中的值。如果不使用這個函數庫,則需要為你的應用手動配置此行為。?
?
?
二、lavarel使用模型進行增刪改查操作
1、相關知識
資源控制器操作處理
| GET | /photos | index | photos.index |
| GET | /photos/create | create | photos.create |
| POST | /photos | store | photos.store |
| GET | /photos/{photo} | show | photos.show |
| GET | /photos/{photo}/edit | edit | photos.edit |
| PUT/PATCH | /photos/{photo} | update | photos.update |
| DELETE | /photos/{photo} | destroy | photos.destroy |
指定資源模型
如果你使用了路由模型綁定,并且想在資源控制器的方法中使用類型提示,你可以在生成控制器的時候使用?--model選項:
php artisan make:controller PhotoController --resource --model=Photo偽造表單方法
因為 HTML 表單不能生成?PUT,PATCH?和?DELETE?請求,所以你需要添加一個隱藏的?_method?字段來偽造這些 HTTP 動作。 這個 Blade 指令 @method 可以為你創建這個字段:
<form action="/foo/bar" method="POST"> @method('PUT') </form>?
2、代碼
表
?
?
?
一、控制器
app/Http/Controllers/Admin/TagController.php
1 <?php 2 3 namespace App\Http\Controllers\Admin; 4 5 use App\Http\Requests\TagRequest; 6 use App\Model\Tag; 7 use Illuminate\Http\Request; 8 use App\Http\Controllers\Controller; 9 10 class TagController extends CommonController 11 { 12 13 /** 14 * Display a listing of the resource. 15 * 16 * @return \Illuminate\Http\Response 17 */ 18 public function index() 19 { 20 // 21 session(['nowControllerAction'=>\App\Model\ControllerAndFunction::jointControllerAndFunction()]); 22 $data=Tag::get(); 23 return view('admin.tag.index',compact('data')); 24 } 25 26 /** 27 * Show the form for creating a new resource. 28 * 29 * @return \Illuminate\Http\Response 30 */ 31 public function create() 32 { 33 return view('admin.tag.create'); 34 } 35 36 /** 37 * 保存新增的數據 38 * Store a newly created resource in storage. 39 * 40 * @param \Illuminate\Http\Request $request 41 * @return \Illuminate\Http\Response 42 */ 43 public function store(TagRequest $request,Tag $model) 44 { 45 $model->create($request->all()); 46 return redirect('/admin/tag'); 47 //dd($request); 48 } 49 50 /** 51 * Display the specified resource. 52 * 53 * @param int $id 54 * @return \Illuminate\Http\Response 55 */ 56 public function show($id) 57 { 58 // 59 } 60 61 /** 62 * Show the form for editing the specified resource. 63 * 64 * @param int $id 65 * @return \Illuminate\Http\Response 66 */ 67 public function edit($id) 68 { 69 $model=Tag::find($id); 70 return view('admin.tag.edit',compact('model')); 71 } 72 73 /** 74 * Update the specified resource in storage. 75 * 76 * @param \Illuminate\Http\Request $request 77 * @param int $id 78 * @return \Illuminate\Http\Response 79 */ 80 public function update(TagRequest $request, $id) 81 { 82 $model=Tag::find($id); 83 $model['name']=$request['name']; 84 $model->save(); 85 return redirect('/admin/tag'); 86 } 87 88 /** 89 * Remove the specified resource from storage. 90 * 91 * @param int $id 92 * @return \Illuminate\Http\Response 93 */ 94 public function destroy($id) 95 { 96 Tag::destroy($id); 97 return response()->json(['message'=>'刪除成功','valid'=>1]); 98 } 99 }第43行和第80行,都用TagRequest進行了驗證
?
?
二、模型
app/Model/Tag.php
1 <?php 2 3 namespace App\Model; 4 5 use Illuminate\Database\Eloquent\Model; 6 7 class Tag extends Model 8 { 9 //$guarded表示不允許批量填充的字段 10 protected $guarded=[]; 11 }第10行,定義這個$guarded字段解決了剔除token字段存數據庫的問題
Tag模型對應數據庫tag表,控制器中就是用Tag進行數據庫操作
?
三、請求
app/Http/Requests/TagRequest.php
1 <?php 2 3 namespace App\Http\Requests; 4 5 use Illuminate\Foundation\Http\FormRequest; 6 7 class TagRequest extends FormRequest 8 { 9 /** 10 * Determine if the user is authorized to make this request. 11 * 12 * @return bool 13 */ 14 public function authorize() 15 { 16 return true; 17 } 18 19 /** 20 * Get the validation rules that apply to the request. 21 * 22 * @return array 23 */ 24 public function rules() 25 { 26 return [ 27 'name'=>'sometimes|required', 28 ]; 29 } 30 31 /** 32 * 中文提示 33 * @return array 34 */ 35 public function messages() 36 { 37 return [ 38 'name.required'=>'標簽名稱不能為空', 39 ]; 40 } 41 }?
?
四、視圖
resources/views/admin/tag/create.blade.php
1 @extends('admin.layout.master') 2 @section('title','新增標簽') 3 @section('content') 4 <!-- Content Header (Page header) --> 5 <section class="content-header"> 6 <h1> 7 Dashboard 8 <small>Control panel</small> 9 </h1> 10 <ol class="breadcrumb"> 11 <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li> 12 <li class="active">Dashboard</li> 13 </ol> 14 </section> 15 16 <!-- Main content --> 17 <section class="content"> 18 <div style="padding-bottom:15px;"> 19 <div class="btn-group" role="group" aria-label="..."> 20 <a href="/admin/tag" type="button" class="btn btn-default">標簽列表</a> 21 <a href="/admin/tag/create" type="button" class="btn btn-warning">新增標簽</a> 22 </div> 23 </div> 24 25 <div class="box box-info"> 26 <div class="box-header with-border"> 27 <h3 class="box-title">Horizontal Form</h3> 28 </div> 29 <!-- /.box-header --> 30 <!-- form start --> 31 <form class="form-horizontal" action="/admin/tag" method="post"> 32 {{csrf_field()}} 33 <div class="box-body"> 34 <div class="form-group"> 35 <label for="name" class="col-sm-2 control-label">標簽</label> 36 37 <div class="col-sm-10"> 38 <input type="text" name="name" class="form-control" id="name" required placeholder="標簽"> 39 </div> 40 </div> 41 42 </div> 43 <!-- /.box-body --> 44 <div class="box-footer"> 45 <button type="submit" class="btn btn-info">保存數據</button> 46 </div> 47 <!-- /.box-footer --> 48 </form> 49 </div> 50 51 </section> 52 <!-- /.content --> 53 @endsection?
resources/views/admin/tag/edit.blade.php
1 @extends('admin.layout.master') 2 @section('title','修改標簽') 3 @section('content') 4 <!-- Content Header (Page header) --> 5 <section class="content-header"> 6 <h1> 7 Dashboard 8 <small>Control panel</small> 9 </h1> 10 <ol class="breadcrumb"> 11 <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li> 12 <li class="active">Dashboard</li> 13 </ol> 14 </section> 15 16 <!-- Main content --> 17 <section class="content"> 18 <div style="padding-bottom:15px;"> 19 <div class="btn-group" role="group" aria-label="..."> 20 <a href="/admin/tag" type="button" class="btn btn-default">標簽列表</a> 21 <a href="/admin/tag/create" type="button" class="btn btn-warning">修改標簽</a> 22 </div> 23 </div> 24 25 <div class="box box-info"> 26 <div class="box-header with-border"> 27 <h3 class="box-title">Horizontal Form</h3> 28 </div> 29 <!-- /.box-header --> 30 <!-- form start --> 31 <form class="form-horizontal" action="/admin/tag/{{$model['id']}}" method="post"> 32 {{csrf_field()}} 33 @method('PUT') 34 <div class="box-body"> 35 <div class="form-group"> 36 <label for="name" class="col-sm-2 control-label">標簽</label> 37 38 <div class="col-sm-10"> 39 <input type="text" name="name" class="form-control" id="name" required placeholder="標簽" value="{{$model['name']}}"> 40 </div> 41 </div> 42 43 </div> 44 <!-- /.box-body --> 45 <div class="box-footer"> 46 <button type="submit" class="btn btn-info">保存數據</button> 47 </div> 48 <!-- /.box-footer --> 49 </form> 50 </div> 51 52 </section> 53 <!-- /.content --> 54 @endsection第33行,請求偽造
?
resources/views/admin/tag/index.blade.php
1 @extends('admin.layout.master') 2 @section('title','標簽頁面') 3 @section('content') 4 <!-- Content Header (Page header) --> 5 <section class="content-header"> 6 <h1> 7 Dashboard 8 <small>Control panel</small> 9 </h1> 10 <ol class="breadcrumb"> 11 <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li> 12 <li class="active">Dashboard</li> 13 </ol> 14 </section> 15 16 <!-- Main content --> 17 <section class="content"> 18 <div style="padding-bottom:15px;"> 19 <div class="btn-group" role="group" aria-label="..."> 20 <a href="/admin/tag" type="button" class="btn btn-warning">標簽列表</a> 21 <a href="/admin/tag/create" type="button" class="btn btn-default">新增標簽</a> 22 </div> 23 </div> 24 25 <div class="box box-primary"> 26 <div class="box-header with-border"> 27 <h3 class="box-title">Bordered Table</h3> 28 </div> 29 <!-- /.box-header --> 30 <div class="box-body"> 31 <div class="table-responsive" style="overflow: visible;min-height: 200px;"> 32 <table class="table table-hover"> 33 <tbody> 34 <tr class="info"> 35 <th style="width: 10px">#</th> 36 <th>標簽</th> 37 <th>操作</th> 38 </tr> 39 @foreach($data as $d) 40 <tr> 41 <td>{{$d['id']}}.</td> 42 <td>{{$d['name']}}</td> 43 <td> 44 <div class="btn-group"> 45 <button type="button" class="btn btn-info">操作</button> 46 <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown"> 47 <span class="caret"></span> 48 <span class="sr-only">Toggle Dropdown</span> 49 </button> 50 <ul class="dropdown-menu" role="menu" style=""> 51 <li><a href="/admin/tag/{{$d['id']}}/edit">編輯</a></li> 52 <li><a href="javascript:;" οnclick="del({{$d['id']}})">刪除</a></li> 53 </ul> 54 </div> 55 </td> 56 </tr> 57 @endforeach 58 59 </tbody> 60 </table> 61 </div> 62 63 </div> 64 <!-- /.box-body --> 65 <div class="box-footer clearfix"> 66 <ul class="pagination pagination-sm no-margin pull-right"> 67 <li><a href="#">?</a></li> 68 <li><a href="#">1</a></li> 69 <li><a href="#">2</a></li> 70 <li><a href="#">3</a></li> 71 <li><a href="#">?</a></li> 72 </ul> 73 </div> 74 </div> 75 76 </section> 77 <!-- /.content --> 78 <script> 79 function del(id){ 80 // require(['util'],function () { 81 // util.confirm('確定刪除么?',function () { 82 // 83 // }); 84 // }); 85 if(confirm('確定刪除么')){ 86 $.ajax({ 87 url:'/admin/tag/'+id, 88 method:'DELETE', 89 success:function (response) { 90 location.reload(); 91 } 92 }); 93 } 94 } 95 </script> 96 @endsection這里的ajax沒有傳token字段是因為 模板主頁里面進行了jquery的token驗證,調用jquery會自動傳token來驗證的
?
?
?
?
轉載于:https://www.cnblogs.com/Renyi-Fan/p/11531274.html
總結
以上是生活随笔為你收集整理的后盾网lavarel视频项目---lavarel使用模型进行增删改查操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DeepStream开发日志
- 下一篇: 【python基础】list操作相关:扩