Do you mean something like distinct()?
https://laravel.com/docs/8.x/queries#specifying-a-select-clause
It just ensures there are no duplicates in your query
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have this mock table rich_guys. Notice there are records with the same slug 1|6 & 3|5. Also 2 slugs have a foo_id 3|6.
+----+------------------------------+------------+
| id | slug | foo_id |
+----+------------------------------+------------+
| 1 | bill-gates | NULL |
| 2 | jeff-bezos | NULL |
| 3 | steve-jobs | 23 |
| 4 | elon-musk | NULL |
| 5 | steve-jobs | NULL |
| 6 | bill-gates | 64 |
| 7 | mark-zuckerberg | NULL |
+----+------------------------------+------------+
How would I write a query so records 1 and 5 are removed and the rest remain?
Simulated output:
+----+------------------------------+------------+
| id | slug | foo_id |
+----+------------------------------+------------+
| 2 | jeff-bezos | NULL |
| 3 | steve-jobs | 23 |
| 4 | elon-musk | NULL |
| 6 | bill-gates | 64 |
| 7 | mark-zuckerberg | NULL |
+----+------------------------------+------------+
Talking it out, I'm thinking I'd need to do something like this, but not sure how to write the query.
get all richGuys
if guy has foo_id and matching slug
show guy with foo_id
if guy has no foo_id and no matching slug
show guy
return richGuys
I'm looking for advice, or a tutorial with this kind of edge case scenario that would help me complete this task. Although I would not frown upon a helpful solution. =)
Please or to participate in this conversation.