@ajmillar When passing data to the view using the compact() function, you need to pass a string referencing the variable name, not the variable itself.
view('view.name', compact('variable'));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a controller that defines and index function to display a list of records from the database as follows
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$organisations = Organisation::all();
return view('organisation.index', compact($organisations));
}
The blade template is as follows
@extends('layout')
@section('title', 'Organisations')
@section('breadcrumb', 'Organisations')
@section('content')
@foreach ($organisations as $organisation)
{{ $organisation->name }}
@endforeach
@stop
However, when I go to run the code, I get the following 2 errors
1/2 ErrorException in d230a6016041e5bce001fd74e18b9554 line 9: Undefined variable: organisations
2/2 ErrorException in d230a6016041e5bce001fd74e18b9554 line 9: Undefined variable: organisations (View: D:\xampp\htdocs\ten25\resources\views\organisation\index.blade.php)
I'm sure I'm missing something obvious, can someone point me in the right direction?
@ajmillar When passing data to the view using the compact() function, you need to pass a string referencing the variable name, not the variable itself.
view('view.name', compact('variable'));
Please or to participate in this conversation.