Blade Syntax Error with empty string variable

Viewed 1244

I have this input:

<input class="form-control" type="text" id="nameEng" name="nameEng" value="{{$tagTrans['en']}}" />

And if the variable is empty I got this message:

Parse error: syntax error, unexpected ''); ?>">' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')'

If it isn't empty everything is perfect.

What is the problem?

EDIT: My Controller@Action is:

public function newTag($id = 0){

    $tag = array();
    $tagTrans = array();

    if ($id > 0){
        $tag = Tags::where(['id' => $id])
                        -> first()
                        -> toArray();

        $tagTransRaw = TagTrans::where(
                                ['tag_id' => $id ])
                            ->get()
                            ->toArray();

        foreach ($tagTransRaw as $key => $tagTransFE) {
            $tagTrans[$tagTransFE['lang']] = $tagTransFE['text'];
        }  
    }

    $data = array(
            'id' => $id,
            'tag' => $tag,
            'tagTrans' => $tagTrans,
        );

    return view('back/news/newTag', $data);
}

EDIT2: Tried other methods:

 - value="{{$tagTrans['en'] or ''}}"
 - value="{{
   !empty($tagTrans['en'])? $tagTrans['en'] : '' }}"

They didn't worked.

EDIT3: I deleted the value="..."-s in the form to show the var_dump($data), what @Mr. Pyramid asked, and now it shows the same error on the end of the file (when it renders the blade templates).

I now thinking about that it's a composer update error what I ran a few hours ago. I checked in git that the updated packages was these:

package name
version from
version to

"name": "laravel/framework", 
"version": "v5.5.19", 
"version": "v5.5.20", 

"name": "nikic/php-parser", 
"version": "v3.1.1", 
"version": "v3.1.2",

"name": "psy/psysh", 
"version": "v0.8.13", 
"version": "v0.8.14", 

"name": "doctrine/instantiator", 
"version": "1.0.5", 
"version": "1.1.0", 

"name": "phpunit/php-code-coverage", 
"version": "5.2.2", 
"version": "5.2.3",

"name": "phpunit/phpunit", 
"version": "6.4.3", 
"version": "6.4.4", 

"name": "sebastian/comparator", 
"version": "2.0.2", 
"version": "2.1.0", 

EDIT4:

I tinkered with my code, but the problem is the same. var_dump($data):

$data = array(
 "id" => 0
  "tag" => []
  "tagTrans" => []
);

The input is now this:

<input class="form-control" type="text" id="nameEng" name="nameEng" value="{{ array_key_exists('en', $tagTrans) ? $tagTrans['en'] : '' }}" />
4 Answers

You can check if the key exists in the array using array_key_exists

<input class="form-control" type="text" id="nameEng" name="nameEng" 
 value="{{ array_key_exists('en', $tagTrans) ? $tagTrans['en'] : '' }}" />

I guess the array data does not contain the key 'en'.

You can try this : value="{{$tagTrans['en'] or ''}}"

Good luck.

Check this out:

{{ empty($tagTrans['en']) ? '' : $tagTrans['en'] }}

I found the error. Some rows upper I had this:

<input type="hidden" name="id" value="{{$id'}}">

Related