meavo meavo - 1 month ago 8
PHP Question

Blade returns undefined offset

I've started learning PHP Laravel and I'm struggling a bit with something (probably quite trivial). When I render my page I see the following error:


ErrorException in BladeCompiler.php line 584: Undefined offset: 1


Controller

Located at \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'));
}

}


View

Located at \App\resources\views\public\company\index.blade.php

@extends('public.layout')

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


When I uncomment
return $companies
in my Controller I do have result, but .. I'm not sure why my - very simple - view isn't rendering. Who can help me out?

Answer

The error point out that there was a issue while compiling blade file possibly because of syntax error. So, just wrap the foreach variable inside paranthesis and the issue should be fixed.

@extends('public.layout')

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