Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jbowman99's avatar

Query Assistance

$users = DB::table('users') ->select('users.name', 'users.id AS user_id', 'new_death_notices.id AS death_notice_id','new_death_notices.status AS notice_status', 'new_death_notices.first_name AS notice_first_name', 'new_death_notices.last_name AS notice_last_name', 'new_death_notices.created_at as notice_created', 'new_death_notices.published_at AS notice_publish', 'basic_obits.id AS basic_id','basic_obits.status AS basic_status', 'basic_obits.first_name AS basic_first_name', 'basic_obits.last_name AS basic_last_name', 'basic_obits.created_at AS basic_created', 'basic_obits.published_at AS basic_publish', 'paid_o_bits.id AS paid_obit_id', 'paid_o_bits.status AS paid_obit_status','paid_o_bits.first_name AS paid_obit_first_name', 'paid_o_bits.last_name AS paid_obit_last_name', 'paid_o_bits.created_at AS paid_obit_created', 'paid_o_bits.published_at AS paid_obit_publish') ->join('new_death_notices', 'users.id', '=', 'new_death_notices.user_id') ->join('basic_obits', 'users.id', '=', 'basic_obits.user_id') ->join('paid_o_bits', 'users.id', '=', 'paid_o_bits.user_id') ->distinct()->get(); dd($users);

i have written this query in an attempt to query 3 tables new_death_notices basic_obits paid_o_bits

each table has an enum value for status i want to display all the new ones in a table in a view,

then all of the edited ones and so on.

currently the query returns duplicate information and I can't figure out why if a user has 3 basic 1 notice and 1 paid i return 3 arrays where the 1 notice and paid are repeated and returned with the 3 different basics

the tables have a lot of identical column names please forgive the AS in the query.

0 likes
1 reply
jimmck's avatar

Can you have both Basic Obits and Paid Obits for the same person? If yes probably the source of the dupes. UNION may be the best bet.

Please or to participate in this conversation.