0

I am trying to get a relationship from a model but only 1 type of children.

Credito is my main model, and it has many archivos. Archivos has a field called 'parent_requisito_id', i want to extract all archivos but that i don't get repeated 'parent_requisito_id' looking for the newest entry of the single parent_requisito_id.

I have no idea into getting that output on a relation.

$files = $credito->archivos;
pato.llaguno
  • 741
  • 4
  • 20

1 Answers1

1

I think this popular post answers your question. But doing the same query with laravel relationship is a bit of work.

I don't know Spanish so I am gonna write some code in English after some translation:

credito: loan
archivo: archive (file)
requisito: requirement

A simple approach could be using Laravel Collection. From the discussion you got some timestamps issue. Lets use id instead of timestamps.

$files = $loan->archives()
    ->get()
    ->keyBy('parent_requirement_id')
    ->values();
Kevin Bui
  • 2,754
  • 1
  • 14
  • 15