<p>Unfortunately this is one of Crafts disadvantages, you'll always fetch all fields in your <code>craft_content</code> table no matter if you need those or not, thus it's always a bit slow and query heavy.</p>

<p>Craft 3 introduces a way to solve that issue since it loops the field layout and only fetches the values you need and not all of them.</p>

<p>Anyway: you could do this:</p>

<pre><code>// get an array of all fields in craft_content (note: that only works for fields
// with a record in `craft_content` so you won't fetch relations with that
$attributes = $journeys[0]-&gt;getContent()-&gt;getAttributes();
$this-&gt;returnJson([
    'title'                 =&gt; $journeys[0]-&gt;getTitle(),
    'myCustomField'         =&gt; $attributes['myCustomField'],
    'myAwesomeField'        =&gt; $attributes['myAwesomeField'],
    'anotherField'          =&gt; $attributes['anotherField'],
    'idsOfARelationField'   =&gt; $journeys[0]-&gt;relationFieldHandle-&gt;ids()
]);
</code></pre>

<p>That way you can define which values you need and return them in an array. If you don't need all the other values just leave them empty. If you only want to fetch all custom fields you can use</p>

<pre><code>$this-&gt;returnJson($attributes);
</code></pre>

<p>Not sure if you'll receive the title that way, you need to try. Othewise - if you need the title and if it's not included</p>

<pre><code>$attributes['title'] = $journes[0]-&gt;getTitle();
$this-&gt;returnJson($attributes);
</code></pre>

<p>solves that.</p>

<p>Edit: by the way if you only need the first element you can do <code>$criteria-&gt;first()</code> instead of <code>$criteria-&gt;find();</code></p>

CC BY-SA 3.0
2018-01-25T16:10:51.123Z
I like your idea of just picking off which pieces you need, and constructing your own response, which is exactly what I am going to do. As for the first() reference, there are going to be multiple entries, I just used [0] as an example in my code. Thanks again.
Dan Zuzevich
CC BY-SA 3.0
2018-01-25T16:13:57.613Z
@DanielZuzevich you could also look into implementing Fractal https://fractal.thephpleague.com, which the Element API plugin also uses
carlcs
CC BY-SA 3.0
2018-01-25T16:16:31.390Z
Not a bad idea. If I only had the time lol.
Dan Zuzevich
CC BY-SA 3.0
2018-01-25T15:50:13.027Z
2018-01-25T16:05:02.733Z
2018-01-25T16:05:02.733Z
Robin Schambach
<p>We are building a React application for a client, where we are using Craft as the API/Backend. I am building the API as a custom Craft plugin, which will return specific JSON data to React, dependent upon the action route it requests. </p>

<p>I was wondering if there was a way that anyone has managed to prevent the default Entry properties from being included in the response, as it just adds a lot of unnecessary clutter. I have included a visual example at the bottom of this question to further clarify what I mean.</p>

<p>I did try to research if this was answered already, and found this link <a href="https://craftcms.stackexchange.com/questions/12513/ignore-fields-in-search-index/13495#13495">Ignore fields in search index</a>
I did see it was back in 2016, so was hoping something has changed.</p>

<p><strong>Example:</strong></p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = "journeys";
$journeys = $criteria-&gt;find();

$responseObj = new \stdClass;
$responseObj-&gt;journeyOne = $journeys[0];
$this-&gt;returnJson($responseObj);
</code></pre>

<p><a href="https://i.sstatic.net/WSpRr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WSpRr.png" alt="enter image description here"></a></p>

<p>I literally won't be using any of these, as I only require the data from my custom fields(which haven't been created yet).</p>

<plugin-development><entries>
Excluding properties from returned ElementModel in plugin
# Excluding properties from returned ElementModel in plugin

(Asked by: username_0 on 2018-01-25)

We are building a React application for a client, where we are using Craft as the API/Backend. I am building the API as a custom Craft plugin, which will return specific JSON data to React, dependent upon the action route it requests. 
I was wondering if there was a way that anyone has managed to prevent the default Entry properties from being included in the response, as it just adds a lot of unnecessary clutter. I have included a visual example at the bottom of this question to further clarify what I mean.
I did try to research if this was answered already, and found this link Ignore fields in search index
I did see it was back in 2016, so was hoping something has changed.
Example:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = "journeys";
$journeys = $criteria->find();

$responseObj = new \stdClass;
$responseObj->journeyOne = $journeys[0];
$this->returnJson($responseObj);


I literally won't be using any of these, as I only require the data from my custom fields(which haven't been created yet).

**Tags:** plugin-development, entries


---

## 1 answers

### Answer by username_1 on 2018-01-25. Score: 4 | Accepted answer

Unfortunately this is one of Crafts disadvantages, you'll always fetch all fields in your craft_content table no matter if you need those or not, thus it's always a bit slow and query heavy.
Craft 3 introduces a way to solve that issue since it loops the field layout and only fetches the values you need and not all of them.
Anyway: you could do this:
// get an array of all fields in craft_content (note: that only works for fields
// with a record in `craft_content` so you won't fetch relations with that
$attributes = $journeys[0]->getContent()->getAttributes();
$this->returnJson([
    'title'                 => $journeys[0]->getTitle(),
    'myCustomField'         => $attributes['myCustomField'],
    'myAwesomeField'        => $attributes['myAwesomeField'],
    'anotherField'          => $attributes['anotherField'],
    'idsOfARelationField'   => $journeys[0]->relationFieldHandle->ids()
]);

That way you can define which values you need and return them in an array. If you don't need all the other values just leave them empty. If you only want to fetch all custom fields you can use
$this->returnJson($attributes);

Not sure if you'll receive the title that way, you need to try. Othewise - if you need the title and if it's not included
$attributes['title'] = $journes[0]->getTitle();
$this->returnJson($attributes);

solves that.
Edit: by the way if you only need the first element you can do $criteria->first() instead of $criteria->find();

> **Comments:**
> **username_0**: I like your idea of just picking off which pieces you need, and constructing your own response, which is exactly what I am going to do. As for the first() reference, there are going to be multiple entries, I just used [0] as an example in my code. Thanks again.  
> **username_2**: @DanielZuzevich you could also look into implementing Fractal https://fractal.thephpleague.com, which the Element API plugin also uses  
> **username_0**: Not a bad idea. If I only had the time lol.  

---

