Getting distinct data based on specific columns

Posted 11 months ago by fogley

Right now, when I run distinct() on my query it will fetch the whole table. I suspect this is because I have timestamps enabled. So even though all the important columns are the same, the query will treat the data as different because it contains different timestamps. I am only interested in the unique data set, disregarding the timestamps.

Is there a way to tell the query builder to only observe specific columns when deciding whether or not data is distinct?

Basically, I need ALL the columns from the table. But ONLY the newest entry and ONLY if all columns EXCEPT created_at/updated_at are identical to any other entry.

Intuitively, I would do it like this:

Users::latest()->distinct('column1', 'column2', 'column3')->get();

Where column1, column2, column3 corresponds to the columns I want to compare to determine if data is distinct.

Please sign in or create an account to participate in this conversation.