Routeを定義している”web.php”でバリテーションを作成して、入力条件にあてはまらなかった場合は、”errors.blade.php”でエラー出力するようにしたいのですが、上手く”errors.blade.php”は呼出せているのですが、そこで以下のようなエラーが出力されます。
Undefined variable: error (View: C:\Users\〇〇〇\laravel\blog\resources\views\common\errors.blade.php)
解決策を教えていただきたいです。
試してみたこと
Laravel5.2からは、以下のコードでRouteを囲むべきだそうで、それを行いました。
php
1Route::group(['middleware' => ['web']], function () { 2省略 3}
web.php
1<?php 2//use Illuminate\Support\Facades\Route; 3use App\Book; 4use Illuminate\Http\Request; 5 6 7Route::group(['middleware' => ['web']], function () { 8/*============================================================================== 9本のダッシュボード表示 10===============================================================================*/ 11Route::get('/', function () { 12 $books = Book::orderBy('created_at', 'asc')->get(); 13 return view('books', [ 14 'books' => $books 15 ]); 16}); 17 18/*============================================================================== 19新しい本の追加 20===============================================================================*/ 21 22Route::post('/books', function (Request $request){ 23 //入力のエラー処理 24 $validator = Validator::make($request->all(),[ 25 'item_name' => 'required|max:255', 26 'item_number' => 'required|min:3 |max:255', 27 ]); 28 29 if($validator->fails()){ 30 return redirect('/') 31 ->withInput() 32 ->withErrors($validator); 33 } 34 35 //Eloquentモデル 36 $books = new Book; 37 $books->item_name = $request->item_name; 38 $books->item_number = $request->item_number; 39 $books->item_amount = '1000'; 40 $books->published = '2017-03-07 00:00:00'; 41 $books->save(); 42 return redirect('/'); // /(ルート)にリダイレクト 43}); 44 45}); 46 47/* 48| 49| 本を削除 50| 51*/ 52 53Route::delete('/book/{book}', function(Book $book){ 54 $book -> delete(); 55 return redirect('/'); 56}); 57 58Auth::routes(); 59 60Route::get('/home', 'HomeController@index')->name('home'); 61
Kernel.php
1<?php 2 3namespace App\Http; 4 5use Illuminate\Foundation\Http\Kernel as HttpKernel; 6 7class Kernel extends HttpKernel 8{ 9 /** 10 * The application's global HTTP middleware stack. 11 * 12 * These middleware are run during every request to your application. 13 * 14 * @var array 15 */ 16 protected $middleware = [ 17 \App\Http\Middleware\TrustProxies::class, 18 \Fruitcake\Cors\HandleCors::class, 19 \App\Http\Middleware\CheckForMaintenanceMode::class, 20 \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 21 \App\Http\Middleware\TrimStrings::class, 22 \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 23 ]; 24 25 /** 26 * The application's route middleware groups. 27 * 28 * @var array 29 */ 30 protected $middlewareGroups = [ 31 'web' => [ 32 \App\Http\Middleware\EncryptCookies::class, 33 \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 34 \Illuminate\Session\Middleware\StartSession::class, 35 // \Illuminate\Session\Middleware\AuthenticateSession::class, 36 \Illuminate\View\Middleware\ShareErrorsFromSession::class, 37 \App\Http\Middleware\VerifyCsrfToken::class, 38 \Illuminate\Routing\Middleware\SubstituteBindings::class, 39 ], 40 41 'api' => [ 42 'throttle:60,1', 43 \Illuminate\Routing\Middleware\SubstituteBindings::class, 44 ], 45 ]; 46 47 /** 48 * The application's route middleware. 49 * 50 * These middleware may be assigned to groups or used individually. 51 * 52 * @var array 53 */ 54 protected $routeMiddleware = [ 55 'auth' => \App\Http\Middleware\Authenticate::class, 56 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 57 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 58 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 59 'can' => \Illuminate\Auth\Middleware\Authorize::class, 60 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 61 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 62 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 63 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 64 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 65 ]; 66} 67
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。