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'];
    }

     

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

         //首页
       public function index()
         {
             return view('teachers.teach');

         }

    }

    teach.blade.php:

    @extends('layouts.app')

    @section('content')
    <a href="{{ route('teach.create') }}" style="padding:5px;border:1px dashed gray;">
    + New Teacher
    </a>
    @endsection

    //实现添加

    public function create()
    {
        return view('teachers.into');
    }

    into.blade.php:

    @extends('layouts.app')

    @section('content')
    <form action="{{ route('teach.store') }}" method="post">
    {{ csrf_field() }}
    {{--//防跨域攻击--}}
    <label>姓名:</label>
    <input type="text" name="name" style="100%;" value="{{ old('name') }}">
    <label>性别:</label>
    <input name="sex" rows="10" style="100%;">{{ old('sex') }}
    <label>电话:</label>
    <input name="tel" rows="10" style="100%;">{{ old('tel') }}
    <button type="submit">OK</button>
    </form>

    @endsection


    //接收数据 入库
    public function store(Request $request){
         $teach=new Teach();
         $teach->name =$request->name;
         $teach->sex = $request->sex;
         $teach->tel = $request->tel;
         if( $teach->save()){
                return $this->into();
         }

    }

    //分页展示数据

    public function into(){
         $users = DB::table('teach')->paginate(3);
          return view('teachers.index', ['users' => $users]);
    }

    index.blade.php:

    @extends('layouts.app')

    @section('content')
    <a href="{{ route('teach.create') }}" style="padding:5px;border:1px dashed gray;">
    + New Teacher
    </a>

    @foreach ($users as $teacher)
        <div style="border:1px solid gray;margin-top:20px;padding:20px;">
         <h2>{{ $teacher->name }}</h2>
         <p>{{ $teacher->sex }}</p>
        <p>{{ $teacher->tel }}</p>
         <p>{{ $teacher->created_at }}</p>
         <p>{{ $teacher->updated_at }}</p>
          <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>

    </div>
       @endforeach
    @endsection

  • 相关阅读:
    sqlhelper使用指南
    大三学长带我学习JAVA。作业1. 第1讲.Java.SE入门、JDK的下载与安装、第一个Java程序、Java程序的编译与执行 大三学长带我学习JAVA。作业1.
    pku1201 Intervals
    hdu 1364 king
    pku 3268 Silver Cow Party
    pku 3169 Layout
    hdu 2680 Choose the best route
    hdu 2983
    pku 1716 Integer Intervals
    pku 2387 Til the Cows Come Home
  • 原文地址:https://www.cnblogs.com/chaihtml/p/10532646.html
Copyright © 2011-2022 走看看