Daniel Doyle
Daniel Doyle

Reputation: 39

Call to a member function has() on array when calling @if ($errors->has()) in blade

I'm trying to return the errors from a form in a blade template using Ardent. This is the function I'm using in my controller:

public function store()
{
  $subscriber = new Subscriber;

  $subscriber->first_name = Input::get('first_name');
  $subscriber->last_name = Input::get('last_name');
  $subscriber->email = Input::get('email');

  if(!$subscriber->save())
  {
    return Redirect::to('/admin/subscriber/create')->with('errors', $subscriber->errors()->all());
  }

  return Redirect::to('/admin/subscriber')->with('status', 1);
}

My ardent rules in the model:

public static $rules = array(
  'email' => 'required|email|unique:users',
  'first_name' => 'required',
  'last_name' => 'required',
);

public static $customMessages = array(
  'first_name.required' => 'First name is required.',
  'last_name.required' => 'Last name is required.',
  'email.required' => 'Email is required.',

  'email.email' => 'Use a real email address!',
  'email.unique' => 'This email address already exists!',
);

And what I'm calling in my blade template:

@if ($errors->has())
    @foreach ($errors->all() as $error)
        <div class='bg-danger alert'>{{ $error }}</div>
    @endforeach
@endif

Every time I try inputting data that should not validate into the form I get the error Call to a member function has() on array which refers to the $errors->has()

Anybody have any idea? Cheers

Upvotes: 2

Views: 7099

Answers (1)

Joel Hinz
Joel Hinz

Reputation: 25384

When you supply the $errors to the view, you use this:

->with('errors', $subscriber->errors()->all())

Which means you have already called the all() method that converts the errors to an array. You'll want to remove the all() call, i.e. just this:

->with('errors', $subscriber->errors())

Then you can use has() and all() in the view like you do.

Upvotes: 5

Related Questions