0

this is my form:

{!! Form::model($countries, ['route' => ['countries.update', $countries->id], 'method' => "PUT"]) !!}
   {{ Form::label('code', 'Country Code:') }}
   {{ Form::text('code', null, ['class' => 'form-control']) }}
   {{ Form::label('name', 'Country Name:') }}
   {{ Form::text('name', null, ['class' => 'form-control']) }}
   {{ Form::submit('Save', ['class' => 'mt-20 btn btn-success btn-sm']) }}
{!! Form::close() !!}

and this is my update function:

$countries = Country::find($id);
$this->validate($request, array(
   'code' => 'required|min:2|max:4',
   'name' => 'required|max:255'
));
$country = Country::where('id',$id)->first();
$country->code = Input::get('code');
$country->name = Input::get('name');
$country->save();
Session::flash('success', 'The Country info was successfully updated.');
return redirect()->route('locations.index', $country->id);

what is the issue in my form that I'm getting Undefined variable: countries error from my blade?

6
  • In the edit function, from where the form is getting called, you must not have passed the $countries variable to the form. Commented Jun 7, 2017 at 3:47
  • Also, if you are passing multiple countries, you can't say $countries->id. Commented Jun 7, 2017 at 3:48
  • no is just edit 1 country when they click on edit button Commented Jun 7, 2017 at 3:48
  • Show us the code for edit function. Commented Jun 7, 2017 at 3:49
  • @linuxartisan thanks man, I was forgotten to put $countries = Country::find($id); in edit function. :) Commented Jun 7, 2017 at 3:52

1 Answer 1

3

Consolidating this answer from our conversation in the comments.

The error Undefined variable: countries in the blade view (form) arises as you have forgotten to pass the said variable to the view.

In the edit function (as this the function calling the view), add the following

$countries = Country::find($id); // though I'd suggest naming it $country
...
return view('<view_name>', compact('countries'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.