0

In the routes.php

Route::get('/form1', 'FriendsController@getAddFriend');
Route::post('/form1', 'FriendsController@postAddFriend');

In the app/Http/Controllers/FriendsController.php

namespace App\Http\Controllers;
use App\Http\Requests\FriendFormRequest; 
use Illuminate\Routing\Controller;
use Response;
use View;

class FriendsController extends Controller
{
public function getAddFriend()
{
    return view('friends.add');
}

public function postAddFriend(FriendFormRequest $request)
{
    return Response::make('Friend added!');
}
}

In the app/Http/Requests/FriendFormRequest.php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Response;

class FriendFormRequest extends Request
{
public function rules()
{
    return [
        'first_name' => 'required',
        'email_address' => 'required|email'
    ];
 }

public function authorize()
{
            return true;
}


public function forbiddenResponse()
{

    return Response::make('Permission denied foo!', 403);
}


public function response()
{

}
}

In the resources/views/friends/add.blade.php

 @foreach ($errors->all() as $error)
    <p class="error">{{ $error }}</p>
 @endforeach

<form method="post">
    <label>First name</label><input name="first_name"><br>
    <label>Email address</label><input name="email_address"><br>
    <input type="submit">
</form>

when i run by http://localhost/laravel/public/form1

I am getting error as "Whoops, looks like something went wrong."

When I remove the following line

 @foreach ($errors->all() as $error)
    <p class="error">{{ $error }}</p>
 @endforeach

It displays the form

What is the error?

Laerte
  • 7,013
  • 3
  • 32
  • 50
vimal ram
  • 133
  • 1
  • 2
  • 8

1 Answers1

2

What I can think of is that your $errors variable is not existing and that's what causes the script to throw an exception.

1. If you are using Laravel 5.2 you might find your answer here: Undefined variable: errors in Laravel

Basically in in app/Http/Kernel.php you need to check if $middlewareGroups['web'] contains

  \Illuminate\View\Middleware\ShareErrorsFromSession::class,

2. If you are using another Laravel version, probably you can add an additional check like this:

     @if(isset($errors))
       @foreach ($errors->all() as $error)
         <p class="error">{{ $error }}</p>
       @endforeach
     @endif

To further investigate the problem you need to give us the stack trace of the exception. If you only see the "Whoops ..." message, then go in you .env file and change APP_DEBUG = true

Community
  • 1
  • 1
iivannov
  • 4,341
  • 1
  • 18
  • 24