//模型层的调用
<?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();
}