【发布时间】:2017-05-29 13:53:15
【问题描述】:
我对 Laravel 很陌生。我在使用 Controller 和新创建的 Blade.php 时发现了这个令人困惑的错误
这是我的控制器
class UsersController extends Controller
{
public function index(){
$users[] = [
'0' => [
'first_name' => 'James',
'last_name' => 'Bond',
'location' => 'UK'
],
'1' => [
'first_name' => 'Jimmy',
'last_name' => 'Carter',
'location' => 'USA'
]
];
return view('admin.users.index', compact('users'));
}
}
这是我的 index.blade.php 文件
@if (count($users) > 0)
@foreach($users as $user)
<li>{!! $user['first_name'] !!}</li>
@endforeach
@endif
我总是收到以下错误。据我所知,我将 $user 对象正确传递给了 Blade.php。但我不知道出了什么问题
ErrorException in f1adbf0485f192729c60772e5dc5b16ad0234be2.php line 3:
Undefined index: first_name (View: /Users/Mac/Development/PHP/Laravel/Lara53/resources/views/admin/users/index.blade.php)
in f1adbf0485f192729c60772e5dc5b16ad0234be2.php line 3
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('/Users/Mac/Development/PHP/Laravel/Lara53/storage/framework/views/f1adbf0485f192729c60772e5dc5b16ad0234be2.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => array(array(array('first_name' => 'James', 'last_name' => 'Bond', 'location' => 'UK'), array('first_name' => 'Jimmy', 'last_name' => 'Carter', 'location' => 'USA'))))) in CompilerEngine.php line 59
at CompilerEngine->get('/Users/Mac/Development/PHP/Laravel/Lara53/resources/views/admin/users/index.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'users' => array(array(array('first_name' => 'James', 'last_name' => 'Bond', 'location' => 'UK'), array('first_name' => 'Jimmy', 'last_name' => 'Carter', 'location' => 'USA'))))) in View.php line 149
【问题讨论】:
标签: php arrays laravel foreach