Laravel ErrorException&即使变量已定义且具有值,也是未定义的变量

时间:2016-10-08 11:13:24

标签: laravel undefined blade

即使我使用compact()通过控制器传递了变量,我也收到了一个Undefined Variable错误,我甚至尝试将变量传递给刀片视图,如return view('address/create', ['addresses' => $addresses]);

路线:

Route::get('/addresses/create', function () {
    return view('address/create');
})->middleware(\App\Http\Middleware\CheckSuperUser::class);

控制器功能:

public function create()
    {
        $addresses = Address::all();
        return view('address/create', compact('addresses'));
    }

查看代码(地址/创建):

<div class="col-md-4">
        <legend>Available Addresses</legend>
        @foreach($addresses as $address)
            {{ $address }}
        @endforeach
</div>

错误讯息:

ErrorException in 656cf590f275580a61ff680434f2a7bc0399cbdb.php line 51:
Undefined variable: addresses (View: C:\Users\xxx\xxx\xxx\dorf\resources\views\address\create.blade.php)

我尝试清除缓存和视图,但这根本没有帮助。重新启动服务器和计算机仍然相同的错误。有什么想法可能会发生什么?

2 个答案:

答案 0 :(得分:1)

@qasimalbaqali你提供了错误的路径你必须像这样使用

Route::get('/addresses/create','ControllerName@create')->middleware(\App\Http\Middleware\CheckSuperUser::class);

ControllerName替换为Controller

我希望这会对你有所帮助。

答案 1 :(得分:0)

尝试通过这种方式

return view('address/create')->with('addresses', $addresses);

更新

@foreach($addresses as $address)
   {{ $address->address }}
@endforeach