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

jgautier's avatar

PHPStorm and passing to Alpine PHP Arrays creates a bug in reading the file

Hi guys,

I use PHPStorm with Laravel IDE and Alpine.js plugin.

I have simple usecase sometimes, for example :

@php
    if(!isset($parameters['servers'])) {
        // get all allowed ftp servers for actual user with all available options
        $ftpServers=\App\Models\Ftpserver::where('ftpdispo', 1)->allowed();
        $exportFtpService=new \App\Services\ExportFtpService();
        $servers=$exportFtpService->getServersWithAllOptions($ftpServers);
    }
    else { // use already known servers (previous movement)
        $servers=$parameters['servers'];
    }

@endphp
<div
    x-data="{
        parameters: @entangle('parameters'),
        servers: {{ json_encode($servers) }},
        init(){
            // skip this step
            {{ $skipThisStep === true ? '$wire.'.$processMove.'();' : '' }}
            $nextTick(() => {
                this.isLoading = false;
                this.isValid = false;
                // parameters.servers already defined (e.g. previous)
                if(this.parameters.servers){
                    // check if there's at least one selected server
                    this.isValid = this.parameters.servers.some(subItem => subItem.selected === true);
                }
                $focus.first();
            });
        },
    }"
    class="..."
>
    <h4>{{ __('step-processes.export_process_step4_headline') }}</h4>
    <form
        id="stepForm{{$currentStepId}}"
        @submit.prevent="submit()"
    >
        <x-sandbox.server-choices :servers="$servers"/>
    </form>
</div>

The problem is that all the JS is not understood (green color for all the x-data's init). If I change the alpine declaration like so :

servers: '{{ json_encode($servers) }}',

By adding quotes, it understands the rest of the JS but the array is not well processed anymore.

I don't know how to fix all of this.

If you have an idea, I am very interested.

0 likes
4 replies
ramonrietdijk's avatar

If you add quotes to the value, JavaScript will interact with it as a string. You should not need the quotes.

The following code should work, as you have in your template.

<div x-data="{
    servers: {{ json_encode($servers) }}
}">

Could you maybe dump the $servers variable, or at least it's structure? Perhaps something is formatted wrong?

ramonrietdijk's avatar

@jgautier Ah got it! I thought the code itself was not working correctly, which I found weird as everything looked fine.

Please or to participate in this conversation.