laravel框架寫的簡(jiǎn)易版的學(xué)生信息管理平臺(tái),貫穿了laravel的控制器、視圖、模板、模型、中間件、路由規(guī)則的使用。
//這是路由文件的關(guān)鍵代碼
Route::group(['middleware' => ['web']], function () {
Route::get('student/index',['uses'=>'StudentController@index']);
Route::any('student/create',['uses'=>'StudentController@create']);
Route::post('student/save',['uses'=>'StudentController@save']);
Route::any('student/update/{id}',['uses'=>'StudentController@update']);
Route::any('student/detail/{id}',['uses'=>'StudentController@detail']);
Route::any('student/delete/{id}',['uses'=>'StudentController@delete']);
});
//控制器文件的關(guān)鍵代碼,增刪改查
class StudentController extends Controller{
//學(xué)生列表
public function index(){
$students = Student::paginate(20);
//dd($students);
return view('student.index',[
'students'=>$students,
]);
}
//新增頁(yè)面
public function create(Request $request){
$student = new Student();
if($request->isMethod('POST')){
//1.控制器驗(yàn)證
/*$this->validate($request,[
'Student.name'=>'required|min:2|max:20',
'Student.age' =>'required|integer',
'Student.sex' =>'required|integer',
],[
'required'=>':attribute 為必填項(xiàng)',
'min'=>':attribute長(zhǎng)度不符合要求',
'integer'=>':attribute必須為整數(shù)',
],[
'Student.name'=>'姓名',
'Student.age' =>'年齡',
'Student.sex' =>'性別'
]);*/
//2.Validator類驗(yàn)證
$validator = \Validator::make($request->input(),[
'Student.name'=>'required|min:2|max:20',
'Student.age' =>'required|integer',
'Student.sex' =>'required|integer',
],[
'required'=>':attribute 為必填項(xiàng)',
'min'=>':attribute長(zhǎng)度不符合要求',
'integer'=>':attribute必須為整數(shù)',
],[
'Student.name'=>'姓名',
'Student.age' =>'年齡',
'Student.sex' =>'性別'
]);
//withInput保持?jǐn)?shù)據(jù)
if($validator->fails()){
return redirect()->back()->withErrors($validator)->withInput();
}
$data = $request->input('Student');
if(Student::create($data)){
return redirect('student/index')->with('success','添加成功');
}else{
return redirect()->back();
}
}
return view('student.create',[
'student'=>$student,
]);
}
//保存數(shù)據(jù)操作
public function save(Request $request){
$data = $request->input('Student');
$student = new Student();
$student->name = $data['name'];
$student->age = $data['age'];
$student->sex = $data['sex'];
if($student->save()){
return redirect('student/index');
}else{
return redirect()->back();
}
}
//更新數(shù)據(jù)操作
public function update(Request $request,$id){
$student = Student::find($id);
if($request->isMethod('POST')){
//Validator類驗(yàn)證
$validator = \Validator::make($request->input(),[
'Student.name'=>'required|min:2|max:20',
'Student.age' =>'required|integer',
'Student.sex' =>'required|integer',
],[
'required'=>':attribute 為必填項(xiàng)',
'min'=>':attribute長(zhǎng)度不符合要求',
'integer'=>':attribute必須為整數(shù)',
],[
'Student.name'=>'姓名',
'Student.age' =>'年齡',
'Student.sex' =>'性別'
]);
//withInput保持?jǐn)?shù)據(jù)
if($validator->fails()){
return redirect()->back()->withErrors($validator)->withInput();
}
$data = $request->input('Student');
$student->name = $data['name'];
$student->age = $data['age'];
$student->sex = $data['sex'];
if($student->save()){
return redirect('student/index')->with('success','修改成功-'.$id);
}
}
return view('student.update',[
'student'=>$student,
]);
}
//信息詳情
public function detail($id){
$student = Student::find($id);
return view('student.detail',[
'student'=>$student,
]);
}
//刪除操作
public function delete($id){
$student = Student::find($id);
if($student->delete()){
return redirect('student/index')->with('success','刪除成功-'.$id);
}else{
return redirect('student/index')->with('error','刪除失敗-'.$id);
}
}
}
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》