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

niyazvpp's avatar

Component Error - Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP)

Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP). This error occurs when I pass a variable with routeName to laravel component. Component File:

/* Component File */
<?php

namespace App\View\Components;

use Illuminate\View\Component;

class adminNavLink extends Component
{

    public $route;

    public $icon;

    public function __construct($route, $icon)
    {
        $this->route = $route;
        $this->icon = $icon;
    }
public function render()
    {
        return view('components.admin-nav-link');
    }
}

Component used:

/*** Component used ***/
			@php
		    	$links = [
		    		[ 'Main Home Page', route('home'), 'fa fa-home'],
		    		[ 'Admin Dashboard', route('admin.dashboard'), 'fad fa-chart-pie'],
		    	];

		    	function isRoute($route) {
		    		return url()->current() == $route[1];
		    	}
		    @endphp

		    @foreach($links as $link)

		    <x-admin-nav-link href="$link[1]" :route="isRoute($link)" :icon="$link[2]">
		        {{ $link[0] }}
		    </x-admin-nav-link>

		    @endforeach

Blade File:

<!-- Blade File -->
<a class="mb-3 capitalize font-medium text-sm hover:text-green-600 transition ease-in-out duration-500 {{ $route ? text-green-600 : '' }}">
    <i class="{{ $icon }} text-xs mr-2"></i>
    {{ $slot }}
</a>
0 likes
2 replies
Snapey's avatar

don't put evaluated code here :route="isRoute($link)"

Either pass $link to the component and do the evaluation inside the component, or evaluate isRoute before using the component

niyazvpp's avatar
niyazvpp
OP
Best Answer
Level 1

@Snapey Thanks for your reply. Evaluating doesn't seems to be the mistake.

Actually, the issue was in component blade file: {{ $route ? text-green-600 : '' }}, There I had to use {{ $route ? 'text-green-600' : '' }} with quotes.

1 like

Please or to participate in this conversation.