//调用模型层
<?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