【问题标题】:Laravel Undefined index foreach at blade.phpBlade.php 中的 Laravel 未定义索引 foreach
【发布时间】: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


    【解决方案1】:

    确保您的 foreach 以这种方式打开和关闭

    @foreach( $recibos as $recibo)
    
    @endforeach
    

    就我而言,我的错误是我将@foreach 结束

    【讨论】:

      【解决方案2】:

      你使用 $users[] = array(),所以$users[0] 保存数组。在你的刀片中应该像这样,或者按照 Paul Androschuk 的回答来实现。

      @if (count($users[0]) > 0)
          @foreach($users[0] as $user)
              <li>{!! $user['first_name'] !!}</li>
          @endforeach
      @endif
      

      【讨论】:

        【解决方案3】:
        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'));
        }
        

        【讨论】:

          猜你喜欢
          • 2018-08-02
          • 2012-08-24
          • 2015-03-23
          • 1970-01-01
          • 1970-01-01
          • 2021-01-07
          • 2020-02-10
          • 1970-01-01
          • 2015-04-06
          相关资源
          最近更新 更多