公共的模板布局和公共的页面一般放在common 里面,
<body> @section('header') {{--头部--}} @show {{--中间内容区域--}} <div class="container"> <div class="row"> {{--左侧布局--}} <div class="col-md-3"></div>
{{--右侧内容区域--}} <div class="col-md-9"> @yield('content') </div> </div> </div> {{--尾部--}} @section('footer') @show</body>
@extends('common.layouts')
@section('content')
@include('common.message')
<div class="panel panel-default">
......
</div>
@stop
1-1 R 查询
页面获取时处理
@foreach($students as $student) <tr> <th scope="row">{{ $student->id }}</th> <td>{{ $student->name }}</td> ..... </tr> @endforeach
model:
<?php namespace App; use IlluminateDatabaseEloquentModel; class Student extends Model { // protected $table = 'student'; public $timestamps = true; public function getDateFormat(){ return time(); } protected function asDateTime($val) { return $val; } }
Controller:
class StudentController extends Controller { //学生列表 public function index(){ // $students = Student::get(); $students = Student::paginate(2); return view('student.index', [ 'students'=>$students, ]); } }
1-2增加
public function create(Request $request) { return view('student.create'); }
<form class="form-horizontal" action="{{Route('student/save')}}" method="post"> <input type="hidden" name="_token" value="{{ csrf_token() }}">
...........
<button type="submit" class="btn btn-primary">提交</button> </form>
Route::any('student/save', 'StudentController@save')->name('student/save');
public function save(Request $request) { $data = $request->input('Student'); $student = new Student(); $student->name = $data['name']; if ($student->save()) { return redirect('student/index'); } else { return redirect()->back(); } }
另一种增加方法(控制器中)
public function create(Request $request) { if ($request->isMethod('POST')) { $data = $request->input('Student'); if (Student::create($data)) { return redirect('student/index')->with('success', '添加成功');// } else { return redirect()->back(); } } return view('student.create'); }
1-2-1 添加提示(在控制器和野蛮操作)
return redirect('student/index')->with('success', '添加成功');//
message页面设置:
{{--成功提示框--}} @if(Session::get('success')) .......... <strong>成功</strong>操作成功提示{{Session::get('success')}} @endif {{--失败提示框--}} @if(Session::has('error')) ................ <strong>失败</strong>操作失败提示 {{Session::get('error')}} @endif
1--2-2 数据验证 (控制器验证)
if ($request->isMethod('POST')) { $this->validate($request, [ 'Student.name'=> 'required|min:2|max:20', 'Student.age'=> 'required|integer', 'Student.sex'=>'required|integer', ]); $data = $request->input('Student');
这里我们要使用中间键组:(kermel.php)中=》=》=》=》错误信息分享到view ---》错误信息放在了session里面,通过
新建页面validator
@if(count($errors)) <div class="alert alert-danger"> <ul> {{--<li>姓名不能为空</li>--}} {{--<li>年龄只能为整数</li>--}} {{--<li>请选择性别</li>--}} @foreach($errors->all() as $error) <li>{{$error}}</li> @endforeach </ul> </div> @endif
create页面插入此页面
@extends('common.layouts') @section('content') @include('common.validator') <!-- 自定义内容区域 --> <div class="panel panel-default">. <div class="panel-heading">新增学生</div>
定制错误提示:【
$this->validate($request, [ 'Student.name' => 'required|min:2|max:20', 'Student.age' => 'required|integer', 'Student.sex' => 'required|integer', ], [ 'required' => ':attribute 为必填项', ], [ 'Student.name' => '姓名', ]); //提示错误信息的定制
】
错误信息提示到缓存中,然后传递到validator页面=》由create页面调用;
1-2-3数据保持{{old('Student.name')}}
1-3 修改模型变动性别
修改模型
const SEX_UN = 10; const SEX_BOY = 20; const SEX_GRIL = 30; public function sex($ind = null){ $arr = [ self::SEX_UN =>'未知', self::SEX_BOY=>'男', self::SEX_GRIL=>'女', ]; if($ind !== null){ return array_key_exists($ind, $arr)?$arr[$ind]:$arr[self::SEX_UN]; } return $arr; }
变动控制器==》student
return view('student.create', [ 'student'=>$student ]);
页面改动
@foreach($student->sex() as $ind=>$val) <label class="radio-inline"> <input type="radio" name="Student[sex]" value="10">未知 </label> @endforeach