刀片返回未定义的偏移量

时间:2016-11-03 21:56:53

标签: php laravel-5 blade

我已经开始学习PHP Laravel了,我正在努力解决一些问题(可能非常简单)。当我渲染页面时,我看到以下错误:

  

BladeCompiler.php第584行中的ErrorException:未定义的偏移量:1

控制器

位于\ App \ Http \ Controllers \ CompanyController.php

namespace App\Http\Controllers;

use App\Company;
use Illuminate\Http\Request;

class CompanyController extends Controller
{

    function index()
    {
        $companies = Company::all();

        // return $companies;
        return view('public.company.index', compact('companies'));
    }

}

查看

位于\ App \ resources \ views \ public \ company \ index.blade.php

@extends('public.layout')

@section('content')
    Companies
    @foreach $companies as $company
        {{ $company->title }}
    @endforeach
@stop

当我在控制器中取消注释return $companies时,我确实有结果,但是......我不确定为什么我的 - 非常简单 - 视图不呈现。谁可以帮助我?

3 个答案:

答案 0 :(得分:5)

错误指出在编译刀片文件时可能由于语法错误而出现问题。 因此,只需将foreach变量包装在paranthesis中,问题就应该解决了。

@extends('public.layout')

@section('content')
    Companies
    @foreach ($companies as $company)
        {{ $company->title }}
    @endforeach
@stop

答案 1 :(得分:1)

这让我发疯。问题是我在注释的代码中加入了以下内容:

// Never ever should you have a @ in comments such as @foreach 
// The reason is the blade parser will try and interpret the @directive
// resulting in a cryptic error: undefined index 1

我希望这对某人有帮助。花了太多时间注释掉我所有的@foreach代码中的 ,才发现最初是导致问题的注释中的指令

答案 2 :(得分:0)

检查$companies是否已设置。

@extends('public.layout')

@section('content')
    @if(isset($companies))
        Companies
        @foreach $companies as $company
            {{ $company->title }}
        @endforeach
    @else
        {{-- No companies to display message --}}
    @endif
@stop
相关问题