//调用模型层

<?php
namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
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 App\Http\Controllers;
use App\School;
use App\teach;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
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="width:100%;" value="{{ old('name') }}">
<label>性别:</label>
<input name="sex" rows="10" style="width:100%;">{{ old('sex') }}
<label>电话:</label>
<input name="tel" rows="10" style="width: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

laravel 实现增 与查的更多相关文章

  1. Laravel框架——增删改查

    增: //save返回true false $res = new member(); res->username = 'a'; $res->password = 'b'; dd($res- ...

  2. Laravel 上手增删改查

    拿到一个框架,除了解框架,还要能实现基本的CURD操作. 添加 1.配置路由,指定添加页面: // routes/web.php 中增加如下: // 添加页面.存放路径 Laravel7/resour ...

  3. laravel——基础增删改查

    一.控制器代码 <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class CurdContro ...

  4. laravel 框架增删改查+数据恢复

    ...............路由页面 //修改页面的自定义路由Route::post('unigoods/updata','uniGoodsController@updata');//数据恢复Rou ...

  5. laravel增删改查

    基本想法是搭建一个FormController,所有以后需要配置生成后台的controller就继承这个FormController就好了.在FormController中定义属性: class Fo ...

  6. laravel orm进行增删改查

    https://laravelacademy.org/post/9699.html 建议用DB门面直接操作数据库,因为ORM性能低.数据查询上面,ORM不会比DB差的,就比如with,是用了sql最基 ...

  7. laravel 使用构造器进行增删改查

    使用原生语句进行增删改查 //$list = DB::select('select * from wt_category where id = :id', ['id' => 34]); //$i ...

  8. laravel 增删改查 数据库设置 路由设置

    laravel 框架的路由设置: url: http://www.shanzezhao.com/laraverl/my_laravel/public/index.php/indexs laravel ...

  9. 仿联想商城laravel实战---5、无刷新的增删改查(动态页面更新的三种方式(html))

    仿联想商城laravel实战---5.无刷新的增删改查(动态页面更新的三种方式(html)) 一.总结 一句话总结: 直接js增加删除修改html 控制器直接返回处理好的页面 用双向绑定插件比如vue ...

随机推荐

  1. 微信小程序组件封装

    第一步,在page下面新建一个template文件,如下图 第二部,在template.wxml中编写公用组件即要封装的代码模块 <!--pages/template/template.wxml ...

  2. C++中的对象初始化

    当对象在创建时获得了一个特定的值,我们说这个对象被初始化.初始化不是赋值,初始化的含义是创建变量赋予其一个初始值,而赋值的含义是把当前值擦除,而以一个新值来替代.对象初始化可以分为默认初始化.直接初始 ...

  3. 重写select样式

    select {/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/appeara ...

  4. K-wolf Number (数位DP)

    题意:求区间内有多少个数满足条件:任意相邻的k个数位都不相等. 思路:老套路 #include<bits/stdc++.h> using namespace std; typedef lo ...

  5. 记账本微信小程序开发六

    记账本微信小程序开发六 我的界面 主界面

  6. JS数据类型判断的方法

    最常用的判断方法:typeof var a='isString'; var b=121221; var c=[1,2,3]; var d=new Date(); var e=function(){ c ...

  7. react脚手架搭建及配置

    npm install -g create-react-app 装完之后,生成一个新的项目,可以使用下面的命令: create-react-app my-app cd my-app/yarn star ...

  8. vue组件传值

    组件的传值(组件之间的通讯) 1.父子通信 1)父传子 传递:当子组件在父组件中当做标签使用的时候,通过给子组件绑定一个自定义属性,值为需要传递的数据 接收:在子组件内部通过props进行接收 接收的 ...

  9. Django之天天生鲜项目

    准备工作 1.配置settings.py内置文件 注意: AUTH_USER_MODEL配置参数要在第一次迁移数据库之前配置,否则可能django的认证系统工作不正常 2.创建应用 3.配置主路由 一 ...

  10. 【BCFTOOLS】按样本拆分VCF文件

    在对vcf的操作有这样三个软件: Vcftools:主要用于群体分析,文本处理的功能不是很强大,虽然这个软件也可以拆分样本,但是这种拆分不涉及文件的处理,只是保留在分析流程里. GATK .x:这个软 ...