Contentful logo

Contentful Community

Filtering entries by reference

I am working on a vintage motorcycle selling website. I want to show all models for a specific brand. My model content-type has a reference to my brand content-type. How can i retrieve all models for a specific brand.

i tried this (Symfony 3.4):
$models = (new Query())
->setContentType(‘model’)
->linksToEntry(‘1LXaGV4PMciKg6USKekISI’);

But this does not work.

according to https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/search-on-references/search-on-references/console/php you can do something like this:

// contentful.php 3.0

$client = new Contentful\Delivery\Client(
  '<content_delivery_api_key>',
  '<space_id>',
  '<environment_id>' // Defaults to "master" if ommitted
);
 
$query = (new \Contentful\Delivery\Query)
    ->setContentType('<content_type_id')
    ->where('fields.brand.sys.contentType.sys.id', '<value>')
    ->where('fields.brand.fields.companyName', 'Lemnos');

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

or using the links_to_entry search parameter like you mentioned, you can do:

// contentful.php 3.0

$client = new Contentful\Delivery\Client(
  '<content_delivery_api_key>',
  '<space_id>',
  '<environment_id>' // Defaults to "master" if ommitted
);

// Using a Query object
$query = (new Contentful\Delivery\Query())
    ->linksToEntry('<entry_id>');
$entries = $client->getEntries($query);

// Alternatively, from a DynamicEntry object
$entry = $client->getEntry('<entry_id>');
$entries = $entry->getReferences();

do either of those work for you?