0

The problem is, it's not just one variable, but statements with @if, @isset and @foreach directives. I can't remove whitespaces without syntax error. And all these whitespaces are displaying in input fields.

In description field I did this:

  1. Check, if old('description') exists.

  2. If yes, display it.

  3. If not, then check, if variable $post exists (I use this form for store and update methods both).

  4. If yes, display it.

  5. If not, the field remains empty.

     <div class="form-group">
     <label for="description">Description</label>
     <textarea id="description" name="description" rows="3">@if(old('description')){{ old('description') }}@else @isset ($post){{$post->description}}@endisset @endif</textarea>
    
     <div class="form-group">
     <label for="tags">Tags</label>
     <input type="text" name="tags" id="tags"
            value="@isset ($post, $tags)@foreach($post->tagged as $tagged){{$tagged->tag_name}},@endforeach @endisset">
    

I tried package hedronium/spaceless-blade, but it doesn't work with input values.

Famaxis
  • 73
  • 1
  • 1
  • 8
  • ErrorException Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` – Famaxis Jan 31 '21 at 13:08
  • Try again `isset(old('description')) ? old('description') : isset($post) ? $post->description : NULL;` – STA Jan 31 '21 at 13:24
  • "isset only works with variables and arrays". I changed `isset(old('description'))` to `old('description')` and get the same error as above. – Famaxis Jan 31 '21 at 13:34
  • 1
    `old('description', $post->description ?? null) ` or `old('description', optional($post)->description)` should work – lagbox Jan 31 '21 at 16:40
  • 1
    @lagbox first works for both methods, second - only for `update`, not `store`. Thanks! – Famaxis Jan 31 '21 at 17:38
  • @lagbox by the way, how to do the same with checkbox? – Famaxis Jan 31 '21 at 18:26

2 Answers2

1

>@if and "@isset will be parsed as string because @ will be parsed as syntax only if not join together with other character except space, new line or tab. You can do if condition without @ inside {{}}.

I have a better solution using ternary operator and null coalescing operator.

change your long code

@if(old('description')){{ old('description') }}@else @isset ($post){{$post->description}}@endisset @endif

to

{{old('description') ?? isset($post)?$post->description:''}}

and change

@isset ($post, $tags)@foreach($post->tagged as $tagged){{$tagged->tag_name}},@endforeach @endisset

to

{{isset($post) ? implode(', ', $post->tagged->pluck('tag_name')->toArray() )):''}}

so the full code:

  <div class="form-group">
  <label for="description">Description</label>
  <textarea id="description" name="description" rows="3">{{old('description') ?? isset($post)?$post->description:''}}</textarea>
</div>


 <div class="form-group">
 <label for="tags">Tags</label>
 <input type="text" name="tags" id="tags"
           value="{{isset($post) ? implode(', ', $post->tagged->pluck('tag_name')->toArray() )):''}}">
</div>
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
  • I changed code, and in description field doesn't display `old('description')`. In tags field appears an error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')' – Famaxis Jan 31 '21 at 12:20
  • old('description') will be shown if you use redirect with `->withInput();` – Muhammad Dyas Yaskur Jan 31 '21 at 12:36
  • for tags I updated the code, just wrong at function arg – Muhammad Dyas Yaskur Jan 31 '21 at 12:37
  • Old description still not working with `withInput()`. In my code it worked without `withInput()`, but with extra spaces. In tag input new error - "Undefined variable: tag_name", because "tagged" from pivot table was not used, I think. – Famaxis Jan 31 '21 at 12:47
  • change `$tag_name` to `$post->tagged` – Muhammad Dyas Yaskur Jan 31 '21 at 12:54
  • `old()` will never working without `withInput()`, maybe you used form validation? it's included `withInput()`. and it's strange if not working, try to change to `{{ old('description') }}` will it show? – Muhammad Dyas Yaskur Jan 31 '21 at 12:57
  • "array_map(): Expected parameter 2 to be an array, object given". "tagged" - it's array of post's tags from other table. I used this package https://github.com/rtconner/laravel-tagging – Famaxis Jan 31 '21 at 12:58
  • Yes, I use validation. With only `{{ old('description') }}` it works. – Famaxis Jan 31 '21 at 13:01
  • so it's a collection, just change to `{{isset($post) ? implode(', ', $post->tagged->pluck('tag_name')->toArray() )):''}}` – Muhammad Dyas Yaskur Jan 31 '21 at 13:05
  • and try to change `??` to `?:` – Muhammad Dyas Yaskur Jan 31 '21 at 13:08
  • I changed `??` to `?:`, and appeared an error: Unparenthesized `a ?: b ? c : d` is deprecated. Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`. Then I added brackets: `{{ (old('description') ?: isset($post))?$post->description:'' }}`. $post value shows, but `old()` - doesn't. – Famaxis Jan 31 '21 at 13:24
  • it's very strange if doesn't work. But sorry I can't replicate your issue without full code. what about tagged, is it worked? – Muhammad Dyas Yaskur Jan 31 '21 at 13:30
  • About tags. I removed one extra bracket and whitespace after comma, and it's work now - `{{isset($post) ? implode(',', $post->tagged->pluck('tag_name')->toArray() ):''}}`. So, half of the problem is solved, thank you. Now tags field doesn't contain extra spaces. – Famaxis Jan 31 '21 at 13:50
  • And about description. When I use `{{old('description') ?? isset($post)?$post->description:''}}` in create form, where there is no variable $post, and when I make a deliberate mistake with validation to test `old()`, I get an error: "Undefined variable: post". When I don't make validation mistakes and have no redirect back, form works well. Only {{ old('description') }} works as it should and returns old value. So, the problem with syntax somewhere here: `{{old('description') ?? isset($post)?$post->description:''}}`, I think. When I change `??` to `?:`, result the same. – Famaxis Jan 31 '21 at 14:33
  • I fix the problem with "description". If you're interested, you can check my own answer to this question. Your answer is accepted anyway, you helped me a lot. – Famaxis Jan 31 '21 at 16:06
0

I solved my problem, using directive @php.

<textarea id="description" name="description" rows="3">@php
        if(old('description')) {
            echo old('description');
        } elseif (isset($post)){
            echo $post->description;
        } 
    @endphp</textarea>

No more extra whitespaces.

Famaxis
  • 73
  • 1
  • 1
  • 8