Lara Lara - 4 months ago 16
MySQL Question

Mysql - SQLSTATE[42S22]: Column not found: 1054 Unknown column

This is my the code of my query :

$payments = DB::table('payments')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')
->leftJoin('clients', 'clients.id', '=', 'payments.client_id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
->where('payments.account_id', '=', Auth::user()->account_id)
->where('payments.is_deleted', '=', false)
->where('invoices.is_deleted', '=', false)
->where('clients.is_deleted', '=', false)
->where('contacts.deleted_at', '=', null);


when I try to show the result of this query it gives me :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'payments_invoices.invoice_id' in 'on clause' (SQL: select `payments`.`payment_date`, `payments`.`amount`, `invoices`.`public_id`, `invoices`.`invoice_number`, `clients`.`name` as `client_name`, `contacts`.`email`, `contacts`.`first_name`, `contacts`.`last_name`, `clients`.`currency_id`, `clients`.`public_id` as `client_public_id`, `clients`.`user_id` as `client_user_id` from `payments` inner join `invoices` on `invoices`.`id` = `payments_invoices`.`invoice_id` inner join `payments_invoices` on `payments`.`id` = `payments`.`payment_id` left join `clients` on `clients`.`id` = `payments`.`client_id` left join `contacts` on `contacts`.`client_id` = `clients`.`id` where `payments`.`account_id` = 1 and `payments`.`is_deleted` = 0 and `invoices`.`is_deleted` = 0 and `clients`.`is_deleted` = 0 and `contacts`.`deleted_at` is null and `contacts`.`is_primary` = 1 order by `payments`.`payment_date` desc limit 50)


knowing that this query works correctly :

$query = DB::table('payments')
->join('accounts', 'accounts.id', '=', 'payments.account_id')
->join('clients', 'clients.id', '=', 'payments.client_id')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')
})
->leftJoin('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')
->where('clients.is_deleted', '=', false)
->where('payments.is_deleted', '=', false)
->where('invitations.deleted_at', '=', null)
->where('invoices.deleted_at', '=', null)
->where('invitations.contact_id', '=', $contactId)
);


what should I do please ??

Answer

This line

   ->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')

Does not make sense.You try join another table using a field from the table itself.So probably you want to join on payments_invoices.payment_id

Then you can try change the order in which you join the tables

$payments = DB::table('payments')
                    ->join('payments_invoices', 'payments.id', '=', 'payments_invoices.payment_id')
                    ->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')

                    ->leftJoin('clients', 'clients.id', '=', 'payments.client_id')
                    ->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
                    ->where('payments.account_id', '=', Auth::user()->account_id)
                    ->where('payments.is_deleted', '=', false)
                    ->where('invoices.is_deleted', '=', false)
                    ->where('clients.is_deleted', '=', false)
                    ->where('contacts.deleted_at', '=', null);