1

I have a variable passed from my controller to my view. And I want to do a check in my view if the variable is empty I show a message if it's not empty I show a div.

This is what I've tried but it gives me a syntax error:

@if(!empty({!! $json !!}))
  <div class=""></div>
@else
  <p>Empty.</p>
@endif

I've also tried

@if(!empty($json))

Doesn't work either. I can't get to see the empty message

Anyone knows what I am doing wrong here?

Many thanks in advance!

1
  • Do dd($json) and see what the value actually is. Commented Jun 3, 2016 at 11:48

3 Answers 3

2

What type are your variable? String?

Try

@if( !empty( json_decode( $json) ) )
Sign up to request clarification or add additional context in comments.

2 Comments

Okay cool this did the trick! Thanks a lot! Did not know it mattered if it's json or not :) Thanks anyways!
I don't think it does really, you just had invalid syntax. You shouldn't use {!! $json !!} because as soon as you use @if() everything is in PHP and PHP doesn't know how to parse {!! !!}. All you really needed to do is remove those.
2

All you need is to:

<?php $json = json_decode($json, true) ?>

somewhere before @if because you gets string "[]" not array so you need to parse json to be array. Best place is to do it in the controller.

You also don't need to escape it inside @if statement:

@if(!empty($json))

2 Comments

Try to dump it please and paste what you get
I get this from the dump "[]"
1

Try this:

@if(count($json) > 0)
  <div class=""></div>
@else
  <p>Empty.</p>
@endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.