Search code examples
laravel-8php-7.4laravel-jetstream

syntax error, unexpected '@', expecting variable (T_VARIABLE) or '{' or '$'


I'am using php 7.4.27, laravel framework 8.83.7 and installed Laravel Jetstream. Login & Register can show. After registered, Then an error notification appears in Laravel: syntax error, unexpected '@', expecting variable (T_VARIABLE) or '{' or '$' and in code line 12 :

<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'jetstream::components.dropdown-link','data' => ['href' => $href,'@click.prevent' => $@clickPrevent]]); ?>

I won't update to php 8. What should i do?

=====edited===== complete error

composer.json file :

{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "php": "^7.3|^8.0",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "laravel/framework": "^8.75",
    "laravel/jetstream": "^2.7",
    "laravel/sanctum": "^2.11",
    "laravel/tinker": "^2.5",
    "livewire/livewire": "^2.5"
},
"require-dev": {
    "facade/ignition": "^2.5",
    "fakerphp/faker": "^1.9.1",
    "laravel/sail": "^1.0.1",
    "mockery/mockery": "^1.4.4",
    "nunomaduro/collision": "^5.10",
    "phpunit/phpunit": "^9.5.10"
},
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
},
"scripts": {
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover --ansi"
    ],
    "post-update-cmd": [
        "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
    ],
    "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "@php artisan key:generate --ansi"
    ]
},
"extra": {
    "laravel": {
        "dont-discover": []
    }
},
"config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true

}


Solution

  • Explanation:

    The issue comes from the fact that the compiled view version under storage/framework/views is failing to evaluate $@clickPrevent.

    <?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'jetstream::components.dropdown-link','data' => ['href' => $href,'@click.prevent' => $@clickPrevent]]); ?>
    

    This has nothing to do with your current PHP version.

    Solution:

    I managed to locally reproduce the issue. Here is how I solved it.

    Steps:

    1. Publish the Jetstream Livewire components.

      Command:

      php artisan vendor:publish --tag=jetstream-views

    2. Open the file resources/views/vendor/jetstream/components/switchable-team.blade.php

      In the file contents,

      Instead of:

       <x-dynamic-component :component="$component" href="#" @click.prevent="$root.submit();"> ❌
      

      Use this:

       <x-dynamic-component :component="$component" href="#"> ✅
      

    Disclaimer:

    Note that this is a temporary solution. Everything works fine for me. As lamented by @IMSoP in a comment, I'm afraid it may affect some unknown functionality. I've opened an issue against the library here:

    switchable-team.blade.php compiled Jetstream component throwing an error: syntax error, unexpected token "@", expecting variable or "{" or "$"


    Update 10th/April/2022:

    Another "more stable" solution stated by @fabpl on the opened Github issue:

    I think it's the same bug like [2.x] Fix parse error caused by alpine shorthand on dynamic-component #1032

    Try to replace @click with x-on:click

    Addendum:

    The issue seems to have been introduced in laravel/jetstream v2.7.2. More specifically Use Alpine's @click.prevent for switching teams.

    The good news is that this has been resolved beginning from laravel/jetstream v2.7.3 onwards. More specifically [2.x] Fix parse error caused by alpine shorthand on dynamic-component.

    I just tagged v2.7.3 which should fix this. Thanks all 👍

    Hence, you may simply just upgrade your Jetstream version to v2.7.3 to get the fix. I.e:

    Command:

    composer update laravel/jetstream:2.7.3

    Sample Output:

    PS C:\Users\Ivan\Documents\SteveContents\GITHUB\Laravel-8.83.7\laravel-8.83.7> composer update laravel/jetstream:2.7.3
    Loading composer repositories with package information
    Info from https://repo.packagist.org: #StandWithUkraine
    Updating dependencies
    Lock file operations: 0 installs, 1 update, 0 removals
      - Upgrading laravel/jetstream (v2.7.2 => v2.7.3)
    Writing lock file
    Installing dependencies from lock file (including require-dev)
    Package operations: 0 installs, 1 update, 0 removals
      - Downloading laravel/jetstream (v2.7.3)
      - Downloading laravel/jetstream (v2.7.3)
      - Downloading laravel/jetstream (v2.7.3)
      - Downloading laravel/jetstream (v2.7.3)
      - Upgrading laravel/jetstream (v2.7.2 => v2.7.3): Extracting archive
    ...