Contentful logo

Contentful Community

Query entry with its assets

#1

Is there a way to query all entry with their assets.

I am using php SDK and i dont want to make a second call to get the assets of each entries.

Thanks !

0 Likes

#2

Hi @dev3,

You can retrieve the content of your linked entries by simply adding an appropriate level of the include parameter.

If you wish to include the field content in the actual parent entry, you can also use the ResolveLink function as described in here and here.

0 Likes

#3

Thank you for your hints,

but i tried using the Include parameter in my query and it did not work…

0 Likes

#4

Could you share with us how exactly you setup your client and query? Please omit any sensitive credentials.

0 Likes

#5

$client = new Client($accessToken, $spaceID, $branchId);

$query = (new \Contentful\Delivery\Query())
->setInclude(2)
->setContentType(<‘content-type’>);

$entries = $client->getEntries($query);

I get one entry from my content-type. This entry contains a media (type: image). But, i dont get any of the information about the media. I only get its id like so:

ResourceArray {#582
-items: array:1 [▼
0 => Entry {#673
#fields: array:4 [▼
“title” => array:1 [:arrow_forward:]
“url” => array:1 [:arrow_forward:]
“image” => array:1 [▼
“en-US” => Link {#668
-linkId: “image-id” <--------------
-linkType: “Asset”
}
]
“img_alt” => array:1 [:arrow_forward:]
]
-localeCode: “en-US”
#localeCodes: array:3 [:arrow_forward:]
#client: Client {#656 :arrow_forward:}
#sys: SystemProperties {#666 :arrow_forward:}
}
]
-total: 1
-limit: 100
-skip: 0
}

Thank you

0 Likes

#6

Hi @dev3,

The issue here is that you haven’t yet tried to access the field. If you try $entry->get('image'), an asset object should be returned.

0 Likes

#7

Hi @gabriel,

I see! but is the $entry->get(‘image’) making a other query?

There is no way to get all the information about the assets of an entry all at once without doing $entry->get(‘xxx’) ?

When an asset contains links to other assets, it is hard to make a generic way to retrieve them all with their sub assets…

Thank you.

0 Likes

#8

Actually, when you set $entry->get(‘image’) that won’t send a request to our API, it will simply access the field value of your retrieved entry.

0 Likes

#9

Alright thank you very much for your help!

0 Likes

#10

@gabriel I have the exact same issue. Could you tell me where i do have to put the $entry->get(‘image’) query / where is this method available?

For example:

$query = new \Contentful\Delivery\Query();
$query->setContentType('event')->setInclude(1);
$events = $client->getEntries($query);
dd($events->get('inspirer'));

throws an exception Call to undefined method Contentful\Core\Resource\ResourceArray::get()
I want to query all event content-types with it’s relations (inspirer).

0 Likes