【问题标题】:ErrorException (E_ERROR) Undefined variable: age (View: /home/....profile.blade.php)ErrorException (E_ERROR) 未定义变量:age(查看:/home/....profile.blade.php)
【发布时间】:2017-10-13 21:47:06
【问题描述】:

我在回收代码上遇到错误,该代码在应用程序的另一部分运行良好..

控制器:

public function edit($id)
{
    $user = User::findOrFail($id);
    $gender = Gender::pluck('name', 'id')->all();
    $age = Age::pluck('group', 'id')->all();
    return view('profile', compact('user', 'gender', 'age'));
}

查看:

<div class="form-group">
    {!! Form::label('age_id', 'Age Group: ') !!}
    {!! Form::select('age_id', $age, null, ['class'=>'form-control']) !!}
</div>

请求:

public function authorize()
{
    return Auth::check();
}

public function rules()
{
    return [
        // 'name'      =>  'required|string|min:4|max:40',
        // 'email'     =>  'required|string|email|min:8|max:40|unique:users',
        // 'photo_id'  =>  'required',
        // 'gender_id' =>  'required',
        // 'age_id'    =>  'required',
        // 'password'  =>  'required|string|min:8|max:16'
    ];
}

【问题讨论】:

  • 您能否更具体地说明发生了什么问题?

标签: php laravel laravel-blade


【解决方案1】:

在“compact()”函数中,您将 3 个变量作为字符串传递:

       return view('profile', compact('user', 'gender', 'age'));

你必须在一个数组中传递它们:

       return view('profile', compact(['user', 'gender', 'age']));

如果这不能修复错误,我将不得不查看模型,但现在是我唯一能看到的。 让我们知道结果。

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 2018-11-29
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多