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 App\View\Components\Notes\NoteTag;
use Illuminate\Support\Facades\Blade;
...
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?