I have a component NoteTag
in app/View/Components/Notes and the blade component in resources/views/components/notes.
I am using this component in a parent component like this:
<x-notes.note-tag :name="$tag->name"></x-notes.note-tag>
NoteTag
has a method which I want to use in the view:
public function typeColor()
{
return substr(md5($this->name), 0, 6);
}
I reference the method in the view like this:
<span style="border: 2px #{{ $typeColor() }} solid" class="rounded-lg mr-2 px-1 bg-gray-200 text-gray-600 shadow">
{{ $name }}
</span>
This works great in local development, but in production i get this:
[previous exception] [object] (ErrorException(code: 0): Undefined variable $typeColor at /home/forge/loggbok.michaelsimsoe.no/storage/framework/views/e354b32f864ce675974b798281d94fe7f4dd2831.php:1)
[stacktrace]
I’ve also tried to pass it to the view as data:
public function render()
{
return view('components.notes.note-tag', ['color' => $this->typeColor()]);
}
Which results in the same error. So I guess there is some mapping issue in production where the component view cant fin the class.
As per https://github.com/laravel/framework/issues/31919 and Laravel docs: Manually Registering Components I’ve tried this in the AppServiceProvider
:
use AppViewComponentsNotesNoteTag;
use IlluminateSupportFacadesBlade;
...
class AppServiceProvider extends ServiceProvider
{
...
public function boot()
{
Blade::component('note-tag', NoteTag::class);
}
...
With no luck.
I’ve seen multiple mentions of this problem:
- SO: laravel 7 blade component public methods not working
- SO: Laravel Components not getting my methods in shared host
- Laracasts.com: laravel 7 blade component public methods not working
Some mention the casing of the class name as an issue. Some mention the manually registering of components. None of them works for me.
I’m using Forge to host the app on DigitalOcean.
I’m using the latest version of Laravel and PHP 8.
Any ideas?
2
Answers
Converting my comment to an answer, because who doesn’t love reputation?! 😉
First things first, have you deleted all files in
storage/framework/views
on the production server?I’d wager that the issue is just a caching one, so
php artisan view:clear
or manually deleting the files in that folder will almost certainly solve this (famous last words)!Aside from that, is there a reason that
app/Views/Components/Notes/NoteTag.php
has a class name ofnoteTag
(with a lowercasen
)?Try changing that to see if that resolves the issue.
Laravel is very particular with it’s naming schemes.
All file names must match their namespace and class names perfectly, this includes casing. This is not the case for views, they can be located anywhere, since they are called in render method with the relative paths.
the only solution worked for me was to move my components to AppViewComponents folder like