drxdn40242 2017-06-07 03:45
浏览 89
已采纳

Laravel编辑表单错误

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?

  • 写回答

1条回答 默认 最新

  • douzhi1972 2017-06-07 03:58
    关注

    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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀