今天整理一天的部署上線沒完成。不過晚上搞了一個目錄或者文章報錯代碼。php
直接將其建立在了Home前臺indexController.php主頁控制器上。css
Route::get('/errors/nothing','Home\indexController@nothing');
在indexController.php文件中建立函數:html
public function nothing() { return view('errors.nothing'); }
在建立函數後,先測試路由是否打通,打通後建立報錯視圖文件。數據庫
創建nothing.blade.php報錯文件。api
<!DOCTYPE html> <html> <head> <title>對不起,不存在!</title> <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css"> <style> html, body { height: 100%; } body { margin: 0; padding: 0; width: 100%; color: #B0BEC5; display: table; font-weight: 100; font-family: 'Lato'; } .container { text-align: center; display: table-cell; vertical-align: middle; } .content { text-align: center; display: inline-block; } .title { font-size: 72px; margin-bottom: 40px; } </style> </head> <body> <div class="container"> <div class="content"> <div class="title">對不起,不存在!</div> </div> </div> </body> </html>
直接在地址欄中運行composer
blog/errors/nothing
顯示運行正常。函數
利用composer建立中間件:測試
php artisan make:middleware CheckAge
建立完成後
撰寫內容google
public function handle($request, Closure $next) { $_arti=Article::where('art_id',$request->art_id)->find($request->art_id); if (!$_arti){ return redirect('errors/nothing'); } return $next($request); }
Route::get('/a/{art_id}','Home\IndexController@article')->middleware('checkArt');
測試經過。code
在建立中間件時候,確實測試了好多代碼。
顯示看網上如何判斷空結果集。
即便取到的空結果集, Eloquent仍然會返回IlluminateDatabaseEloquentCollection對象實例。這個我曾經也測試過,確實dd()測試以後含有結果集輸出,只是輸不了數據庫中的字段內容,因此採用if()字段判斷時,依然失效。
其實,Eloquent已經給咱們封裝幾個判斷方法。
$result = Model::where(...)->get(); //不爲空則 if ($result->first()) { } if (!$result->isEmpty()) { } if ($result->count()) { }
可是使用->get()顯示這是一個無定義字段,後來發現find()可使用,具體緣由待好好看手冊再分析。
參考:
在Laravel Eloquent 判斷取出的結果集是否爲空http://www.cnblogs.com/wuoshi...和Eloquent collection: counting and detect empty http://stackoverflow.com/ques...中代表: