Answers.Body: <p>It depends on what type of field <code>productApiId</code> is. If it's a simple number or text field, your code should work as is. But if it's a more complex field – for example, an Entries field, a Categories field, a Matrix field or a Super Table field – then <code>getFieldValue</code> will may return an object instead of a scalar value. All element relation fields (entries, categories, assets etc.) in particular return an <a href="https://craftcms.com/docs/4.x/element-queries.html" rel="nofollow noreferrer"><code>ElementQuery</code> object</a>. For those fields, you need to check the <em>result</em> of the query to see if the field contains anything.</p>
  <p>For example, if <code>productApiId</code> is an <a href="https://craftcms.com/docs/4.x/entries-fields.html" rel="nofollow noreferrer">Entries field</a>, you can check if the field has any elements selected like this:</p>
  <pre class="lang-php prettyprint-override"><code>$entry-&gt;getFieldValue('productApiId')-&gt;exists();
  </code></pre>
  <p>For all fields in general, check the documentation to see what kind of value they return. Some of those may be objects with their own way to tell if the object represents an empty value.</p>
  <p>To find out what kind of value your field returns, you can use the useful debugging method <a href="https://docs.craftcms.com/api/v4/craft.html#method-dd" rel="nofollow noreferrer"><code>Craft::dd</code></a>:</p>
  <pre class="lang-php prettyprint-override"><code>\Craft::dd($entry-&gt;getFieldValue('productApiId'));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-25T20:31:42.710Z
Answers.Text: The productApiId field is a simple text! Unfortunately, but my code doesn't work. There are only two entries in my section. One has the field filled in, the other does not. The message "field not empty" prints only once. The message "field empty" does not output((
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-25T20:47:24.233Z
Answers.Text: this construction       if ($entry->getFieldValue('productApiId')){      unable to return false.  How to check for false?
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-26T08:26:02.913Z
Answers.Text: @Dimi That code is correct, you're likely seeing the effects of a secondary issue. Sometimes it's something simple like having a single space inside the text field, so it's technically not empty. Use `Craft::dd` as mentioned in the post to find out what `getFieldValue` is returning, this will tell you what's going wrong.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-27T18:17:42.367Z
Answers.Text: thx, this info is usefull for me))
Answers.UserDisplayName: Dimi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-25T07:45:07.803Z
Answers.LastActivityDate: 2022-10-25T07:45:07.803Z
Answers.UserDisplayName: MoritzLost
Body: <p>I use Craft CMS 4.2.5.2. I have a simple question, but I can't find an answer to it. I have a module. In it, I wrote a controller that runs through all the elements of the section and performs some actions on each element of the section. I need a condition with which I can check the element field for emptiness. I wrote this:</p>
  <pre><code>$entries = Entry::find()-&gt;section('products');
  
  foreach($entries as $entry){
      if ($entry-&gt;getFieldValue('productApiId')){
          echo &quot;field not empty&lt;br /&gt;&quot;;
      }else{
          echo &quot;field empty&lt;br /&gt;&quot;;
      }        
  }
  </code></pre>
  <p>But this design does not work. If the field is empty, the message &quot;field empty&quot; is not displayed. Tell me please.</p>
  
Tags: <craft3>
Title: How do I check the field for emptiness?
ThreadText: # How do I check the field for emptiness?
  
  (Asked by: username_0 on 2022-10-24)
  
  I use Craft CMS 4.2.5.2. I have a simple question, but I can't find an answer to it. I have a module. In it, I wrote a controller that runs through all the elements of the section and performs some actions on each element of the section. I need a condition with which I can check the element field for emptiness. I wrote this:
  
  ```
  $entries = Entry::find()->section('products');
  
  foreach($entries as $entry){
      if ($entry->getFieldValue('productApiId')){
          echo "field not empty<br />";
      }else{
          echo "field empty<br />";
      }        
  }
  
  ```
  
  But this design does not work. If the field is empty, the message "field empty" is not displayed. Tell me please.
  
  **Tags:** craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-10-25. Score: 0 | Accepted answer
  
  It depends on what type of field `productApiId` is. If it's a simple number or text field, your code should work as is. But if it's a more complex field – for example, an Entries field, a Categories field, a Matrix field or a Super Table field – then `getFieldValue` will may return an object instead of a scalar value. All element relation fields (entries, categories, assets etc.) in particular return an [`ElementQuery` object](https://craftcms.com/docs/4.x/element-queries.html). For those fields, you need to check the *result* of the query to see if the field contains anything.
  
  For example, if `productApiId` is an [Entries field](https://craftcms.com/docs/4.x/entries-fields.html), you can check if the field has any elements selected like this:
  
  ```php
  $entry->getFieldValue('productApiId')->exists();
  
  ```
  
  For all fields in general, check the documentation to see what kind of value they return. Some of those may be objects with their own way to tell if the object represents an empty value.
  
  To find out what kind of value your field returns, you can use the useful debugging method [`Craft::dd`](https://docs.craftcms.com/api/v4/craft.html#method-dd):
  
  ```php
  \Craft::dd($entry->getFieldValue('productApiId'));
  
  ```
  
  > **Comments:**
  > **username_0**: The productApiId field is a simple text! Unfortunately, but my code doesn't work. There are only two entries in my section. One has the field filled in, the other does not. The message "field not empty" prints only once. The message "field empty" does not output((  
  > **username_0**: this construction       if ($entry->getFieldValue('productApiId')){      unable to return false.  How to check for false?  
  > **username_1**: @username_0 That code is correct, you're likely seeing the effects of a secondary issue. Sometimes it's something simple like having a single space inside the text field, so it's technically not empty. Use `Craft::dd` as mentioned in the post to find out what `getFieldValue` is returning, this will tell you what's going wrong.  
  > **username_0**: thx, this info is usefull for me))  
  
  ---
  
  