//调用模型层

<?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. [py]js前端求和与flask后端求和

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. [svc]unix和cpu发展历史

    最近搞汇编 , 有一款8086cpu,16bit, 支持内存1M 于是勾起了对计算机历史的兴趣,多了解了下 unix起源历史 [Unix发展历史 - 程序猿-贝岩博客 - CSDN博客]https:/ ...

  3. idea 无法找到或加载主类

  4. Python返回多个值

    def get_abc(): a = 1 b = 2 c = 3 return a,b,c temp = get_abc() #temp = (1,2,3) a,b,c = get_abc() #a ...

  5. 谷歌PM面试官告诉你,如何成功拿到理想offer

    快来看看大咖Nick如何甄选人才~文章转自墨刀公众号. 本文作者: Nick Baum,目前在谷歌做PM 作为谷歌的产品经理和面试官,我已经面过几百个PM的候选人了吧.这些经历能让我分享一下在PM求职 ...

  6. 遇到NotificationService: Suppressing notification from package com.example.dell.servicebestpractice by u错误

    一般来说是手机设置没有允许通知

  7. 四、latex字体字号设置

    latex的思想是格式与内容的分离,所以不建议在文中使用大量命令,而是定义一个新的命令

  8. 虚拟机与Docker有何不同

    http://www.techug.com/post/comparing-virtual-machines-vs-docker-containers.html 译者按: 各种虚拟机技术开启了云计算时代 ...

  9. 221. Maximal Square(动态规划)

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  10. 【转】python虚拟环境--virtualenv

    virtualenv 是一个创建隔绝的Python环境的工具.virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包. 安装 pip install virtu ...