【问题标题】:Laravel 5 variable not being recognisedLaravel 5变量未被识别
【发布时间】:2017-03-05 06:42:08
【问题描述】:

有点奇怪的问题,这可能是因为我正在使用 Laravel。

我有一个编辑页面,可通过以下方式访问:

Route::get('editRow/{id}', function($id){
    $section = App\Section::where('id', $id)->with('panels')->first();
    return view('front.editrow',['thesection'=>$section]);
});

这个表格然后被完成并进入一个带有 longish 方法的控制器来更新表格,并以:

return view('front.editrow',['message'=>'update successful','id'=>$request->id]);

数据库更新正常。最初我遇到了一个错误,所以我复制了路线并将其从 get 更改为 post:

Route::post('editRow/{id}', function($id){
    $section = App\Section::where('id', $id)->with('panels')->first();
    return view('front.editrow',['thesection'=>$section]);
});

现在虽然这是完全相同的参数等,但我总是得到;

Undefined variable: thesection

我很困惑,如果能解开这个谜团,我将不胜感激!

【问题讨论】:

    标签: php laravel variables laravel-5 routing


    【解决方案1】:

    确保始终将 thesection 变量传递给视图。

    当你这样做时:

    return view('front.editrow',['message'=>'update successful', 'id'=>$request->id]);
    

    未提供此变量。您需要在视图中提供它或在刀片模板中检查它是否存在:

    @if(isset($thesection)
      // whatever you want to do with the section
    @endif
    

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 1970-01-01
      • 2023-02-08
      • 2013-01-16
      • 1970-01-01
      • 2015-03-03
      • 2019-04-29
      • 2018-09-09
      • 2017-03-15
      相关资源
      最近更新 更多