Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

diegoarbelaez's avatar

Undefined Variable - Laravel 7 - Components

I have a weird issue where i'm setting a variable for a Laravel 7 Component to be displayed in the component. While it works in my development environment (MAMP on MacOS) it does not work when i load it to my shared hosting in HostGator. In HostGator it throws a 500 error complaining about "Undefined Variable: page_title".

Any ideas as to what could be happening here?

<!-- App/Components/Layout/ContentHeader.php -->
<?php

namespace App\View\Components\layout;

use Illuminate\Support\Facades\Request;
use Illuminate\View\Component;

class ContentHeader extends Component
{
	/**
	 * @var string
	 */
	public $page_title;
	
	/**
	 * @var array
	 */
	public $breadcrumbs = [];
	
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
	$this->page_title = 'Some title';
	$crumb = new \stdClass();
	    $crumb->label = 'Home';
	    $crumb->active = false;
	    $this->breadcrumbs[] = $crumb;
		   
	    $crumb = new \stdClass();
	    $crumb->label = $this->page_title;
	    $crumb->active = true;
	    $this->breadcrumbs[] = $crumb;
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('components.layout.content-header');
    }
}
<!-- Resources/views/components/layout/content-header.blade.php -->
<div class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1 class="m-0 text-dark">{{ $page_title }}</h1>
			</div><!-- /.col -->
			<div class="col-sm-6">
				<ol class="breadcrumb float-sm-right">
					@foreach ($breadcrumbs as $crumb)
						@if (!$crumb->active)
							<li class="breadcrumb-item"><a href="#">{{ $crumb->label }}</a></li>
						@else
							<li class="breadcrumb-item active">{{ $crumb->label }}</li>
						@endif
					@endforeach
				</ol>
			</div><!-- /.col -->
		</div><!-- /.row -->
	</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->```
0 likes
15 replies
Ksandar's avatar

Maybe a cache? Try run

php artisan view:clear

on the server

2 likes
Ksandar's avatar

Well, then maybe the problem in opcache

in this case opcache_reset() may help (or restarting the server).

I tried your code locally and it works fine. So I think the problem is in the server settings, not in your code

jeevamugunthan's avatar

you need to send page_title variable to your header blade

please show your route

diegoarbelaez's avatar

@jeevamugunthan as noted in the description this does work in development but does not work in a shared hosting environment - leads me to believe it is something with a server setting or missing feature. Thanks for the input though.

@ksandar opcache does not seem to be enabled on my shared hosting - at least the function is not available.

Ksandar's avatar

@diegoarbelaez it seems there are a bug with new blade components on some environments (see this issue)

btw, the problem may be in the class constructor. Try change your ContentHeader.php - rename $page_title to $pageTitle and add it to constructor attributes like this

<?php

namespace App\View\Components\layout;

use Illuminate\Support\Facades\Request;
use Illuminate\View\Component;

class ContentHeader extends Component
{
	/**
	 * @var string
	 */
	public $pageTitle;

         /**
	 * @var array
	 */
	public $breadcrumbs = [];
	
	/**
     	* Create a new component instance.
     	*
     	* @return void
     	*/
    	public function __construct($pageTitle = 'Some title')
    	{
       		//
		$this->pageTitle = $pageTitle;
		$crumb = new \stdClass();
	    	$crumb->label = 'Home';
	    	$crumb->active = false;
	    	$this->breadcrumbs[] = $crumb;
		   
	    	$crumb = new \stdClass();
	    	$crumb->label = $this->page_title;
	    	$crumb->active = true;
	    	$this->breadcrumbs[] = $crumb;
    	}

	/**
     	* Get the view / contents that represent the component.
     	*
     	* @return \Illuminate\View\View|string
     	*/
    	public function render()
    	{
        	return view('components.layout.content-header');
    	}
}
 	

And change content-header.blade.php accordingly

...
<h1 class="m-0 text-dark">{{ $pageTitle }}</h1>
...

And also run composer update if you use laravel less than v7.1.3 (or even run it anyway)

diegoarbelaez's avatar

Thanks @ksandar , it does work if i pass the variable via the component tag and does seem to be ignoring the constructor, almost seems as if the component constructor is not being called.

Thanks for the link - issue does sound similar i'll follow up with them as i'm already on the Laravel mentioned there - will provide updates once resolved.

jeevamugunthan's avatar

@diegoarbelaez ok fine do this it maybe works

inject your controller in header blade like

@inject('pro', 'App\Http\Controllers\DashboardController')
{{$pro->__construct() }}

GuntarV's avatar

Weird issue! I came across it as well.

Tried

composer dump-autoload

php artisan view:clear

that did not help.

Renaming the view class to something else and renaming it back fixed the issue for me.

1 like
Patriksandgren's avatar

I have the exact same problem in laravel 9. Cannot access variable that is defined in constructor (or in render()).

My workaround was so define the variable in the blade file instead. Not very good - but works.

Like this in .blade.php file

Please or to participate in this conversation.