实现方法:
M:对数据库的连接和方法的定义
V:控制输出什么网页(视图)
C:控制器
测试工具:使用Subilme
效果图:
添加页面
显示页面(学生列表)
删除后
编辑信息修改
更改后
更改后的储存到数据库内容
mvc框架的创建,这里用命令提示符
M:php artisan make:Model Models\StuModel
C:php artisan make:Controller StudentController --resource //自带七个方法
创建的路由
Route::resource('/xue','StudentController');
1.首先创建一个数据库db1,然后创建一张表stu(字段为id,name,class,address)用来储存我们添加的数据
mvc框架:
M---从Model入手,封装操作数据库的方法一张数据表对应一个Model(StuModel)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class StuModel extends Model
{
//
protected $table ="db1.stu";
protected $primaryKey = "id";
protected $fillable = ['name','class','address'];//$fillable允许写入和修改
public $timestamps =false;//新的Model没有时间戳,timestamps添加一条记录,系统会给一个时间,标志什么时候修改,或者添加这条记录,现在设置false则不需要这样的操作,
public function add($data)
{
return self::create($data);//self::create($data)等价与insert into userinfo() vaules(),self意思说extends Model里面----Model封装了增删查改的方法
//insert into userinfo(数组下标) values(数组的值)
//所以需要把视图的控件名称和数据表字段一致(包括大小写)
}
public function getall(){
return self::all(); //select *from stu;//获取所有数据
}
public function del($id){//以id查找到数据删除数据,
return self::find($id)->delete();//返回一个model本身的find()方法,并以id查找,查找到返回给delete()
}
public function getone($id){//查找
return self::find($id);
}
public function updatedata($data,$id){//更改
return self::find($id)->update($data);
}
}
C:StudentController控制器的主要作用是接收用户请求,调用模型处理数据,最后通过视图展示数据
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\StuModel;//引用模型
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()//展示所有记录
{
$stum=new StuModel();
$list=$stum->getall();//大批量的数据交给list
//var_dump($list);
return view("Stu.index")->with('li',$list);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() //弹出一个视图,提供输入信息的界面
{
//
// $str='这是create方法';
// echo $str;
return view('Stu.create');//Stu.create
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)//从create界面获取信息,实现insert into功能
{
$data=$request->only('name','class','address');
//var_dump($data);
$stum=new StuModel();//创建模型对象
$stum->add($data);//调用对象方法
return redirect('xue');//返回给视图
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)//根据id进行查询,select*from 表名 where id = $id
{
//
$str ="这是show方法";
echo $str;
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)//编辑页面,
{
$stum=new StuModel();
$one=$stum->getone($id);
//var_dump($one);
return view('Stu.edit')->with('one',$one);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)//更改数据,
{
$data=$request->only('name','class','address');//获取批量数据
$stum=new StuModel();
$stum->updatedata($data,$id);
return redirect('/xue');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//echo($id);
$stum=new StuModel();
$stum->del($id);
return redirect('/xue');
}
}
V:views视图,呈现结果页面
index.blade.php//学生列表页面
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
</head>
<body>
<div align="center">
<form method="POST" action="/xue">
{{csrf_field()}}
<table border="3" width="300px" height="200px">
<caption>学生列表</caption>
<tr>
<td>id</td>
<td>姓名</td>
<td>班级</td>
<td>地址</td>
<td>操作</td>
<td>修改</td>
</tr>
@foreach($li as $one)
<tr>
<td>{{$one->id}}</td>
<td>{{$one->name}}</td>
<td>{{$one->class}}</td>
<td>{{$one->address}}</td>
<td><form action="/xue/{{$one->id}}" method="POST">
{{csrf_field()}}
{{method_field('delete')}}
<button value="submit" type="submit">delete</button>
</form></td>
<td><a href="/xue/{{$one->id}}/edit">编辑</a></td>
</tr>
@endforeach
</table>
</form>
<a href="/xue/create">返回</a>
</div>
</body>
</html>
create.blade.php//添加学生信息页面
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
</head>
<body>
<div align="center">
<form method="POST" action="/xue">
{{csrf_field()}}//表单令牌
<table>
<caption>添加学生</caption>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>班级</td>
<td><input type="text" name="class"></td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td><input type="submit" name="提交"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
edit.blade.php//编辑学生信息页面
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<style>
</style>
</head>
<body>
@if($errors->count() > 0)
{{$errors->first()}}
@endif
<form method="POST" action="/xue/{{$one->id}}">
{{csrf_field() }}
{{method_field('patch')}}//给表单增加一个patch请求
<table>
<caption>编辑学生</caption>
<tr><td>姓名</td><td><input type="text" name="name" value="{{$one->name}}"></td></tr>
<tr><td>班级</td><td><input type="text" name="class" value="{{$one->class}}"></td></tr>
<tr><td>地址</td><td><input type="text" name="address" value="{{$one->address}}"></td></tr>
<tr><td><input type="submit" value="提交"></td></tr>
</table>
</form>
</body>
</html>