Search code examples
phplaravelnginxlaravel-livewire

Undefined variable $form on remote server


I created simple app and after deploying app on remote server i have issue with code.

But.. on my local machine this code works fine, i even pulled code again from git and installed again on my local machine and it's working without any issues.

Error that i have after visiting app.com:

Undefined variable $form

web.php:

Route::view('/', 'home')
    ->middleware(['auth']);

View home.blade.php

...
    <div>
        <livewire:ListQrcode />
    </div>

Livewire component: ListQrcode.php

class ListQrCode extends Component
{
    public $qrCodes;
    public QrCode $selectedQrCode;
    public string $selectedTitle = '';
    public string $selectedDescription = '';
    public string $selectedCode = '';
    public bool $createQrcode = false;
    public bool $showDrawer2 = false;
    public CreateQrcode $form;

    public function render()
    {
        $this->qrCodes = QrCode::with('tags')->orderBy('created_at', 'desc')->get();
        return view('livewire.ListQrcode',
            ['qrCodes' => $this->qrCodes]
        );
    }
}

Livewire Form:

class CreateQrcode extends Form
{
    #[Validate('required', 'string', 'min:3')]
    public $code = '';

    #[Validate('required', 'string', 'max:255')]
    public $title = '';

... 
}

View that code throw error,

                <div class="flex justify-center min-h-[296px] p-8">
                    @if($form->code)
                        <div class="p-4 dark:bg-gray-600 rounded-lg">
                            {!! QrCode::size(200)->generate($form->code) !!}
                        </div>
                    @endif
                </div>

From some reason, PHP don't see $form variable on remote machine, when i comment problematic variable in template, then i have issue with another variable that is undefined.

What i should check to resolve that problem? It's seems that PHP have issues with visibility of variables somehow.

Local machine:

  • Windows 11
  • NGINX
  • PHP 8.3

Remote machine:

  • Ubuntu 23
  • NGINX
  • PHP 8.3

Solution

  • So, I finally resolved issue.

    On one discord server (LaravelDaily), one user pointed to file name collisions as the cause. Windows ignores such problems, on the other hand, for Linux it is a problem.

    The problem was in the naming of two files:

    • app/Livewire/ListQrCode.php (correct name)
    • resources/views/livewire/ListQrCode.blade.php ( should be list-qr-code.blade.php)

    And in the home.blade.php file <livewire:ListQrcode /> should be <livewire:list-qr-code />

    The whole problem could have been avoided if I had followed the correct naming convention :)