zoukankan      html  css  js  c++  java
  • laravel框架实现数据的删除和修改

    //模型层的调用

    <?php
    namespace App;
    use IlluminateSupportFacadesDB;
    use IlluminateDatabaseEloquentModel;
    class teach extends Model
    {
    //数据表
       public $table='teach';
    //定义主键
       public $primarkey='id';
    //时间戳
       public $timestamps=true;
    //白名单
        protected $fillable=['updated_at','created_at','name','sex','tel','id'];
    }

    //展示页面

    <a href="{{ route('teach.edit', $teacher->id ) }}">Edit</a>//跳转修改方法
    <form action="{{ route('teach.destroy', $teacher->id) }}" method="post" style="display: inline-block;">
          {{ csrf_field() }}
           {{ method_field('DELETE') }}
    <button type="submit" style="color: #F08080;background-color: transparent;border: none;">Delete</button>//跳转删除方法
    </form>

    <?php
    namespace AppHttpControllers;
    use AppSchool;
    use App each;
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesDB;
    class TeachController extends Controller
    {

    //引入修改页面
    public function edit($id)
    {
          $teacher = Teach::findOrFail($id);
          return view('teachers.edit', compact('teacher'));

    }

    edit.blade.php:

    @extends('layouts.app')

        @section('content')
    <form action="{{ route('teach.update', $teacher->id) }}" method="post">
         {{ csrf_field() }}
         {{ method_field('PATCH') }}

    <!-- //这是跨站方法伪造,HTML 表单没有支持 PUT、PATCH 或 DELETE 动作。所以在从 HTML 表单中调用被定义的 PUT、PATCH 或 DELETE 路由时,你将需要在表单中增加隐藏的 _method 字段来伪造该方法
    -->
       <label>姓名:</label>
       <input type="text" name="name" style="100%;" value="{{ old('name') }}">
       <label>性别:</label>
       <input name="sex" rows="10" style="100%;" value="{{ old('sex') }}">
       <label>电话:</label>
         <input name="tel" rows="10" style="100%;" value="{{ old('tel') }}">
         <button type="submit">OK</button>
    </form>
    @endsection

    //修改
    public function update(Request $request, $id){
        $this->validate($request, [
                'name' => 'required|max:50',
         ]);

         $teacher = Teach::findOrFail($id);
         $teacher->update([
             'name' => $request->name,
             'sex' => $request->sex,
             'tel' => $request->tel,
       ]);
    }

    //删除

    public function destroy($id)
    {
    $teacher = Teach::findOrFail($id);
    $teacher->delete();
    return back();
    }

  • 相关阅读:
    RTT startup.c 代码学习
    [RTT例程练习] 1.2 静态线程除初始化与脱离
    linux 下生成核心文件
    [RTT例程练习] 2.3 信号量检测按键(同步) (信号量互斥)
    [RTT例程练习] 1.6 线程优先级反转原理
    [RTT例程练习] 1.7 优先级翻转之优先级继承
    extern int Image$$RW_IRAM1$$ZI$$Limit
    [RTT例程练习] 1.1 动态线程创建,删除
    [RTT例程练习] 1.3 线程让出
    Linux操作系统下三种配置环境变量的方法
  • 原文地址:https://www.cnblogs.com/chaihtml/p/10532721.html
Copyright © 2011-2022 走看看