У меня отношение один к одному полиморф c.
customer_class_bookings
id | customer_id | status | total | quantity | trader_user_id
Polymorphic table (trader_job_payment)
id | status | payable_id | payable_type
Мой запрос
$earnings = CustomerClassBooking::where('trader_user_id', $user_id)
->leftjoin('trader_job_payment', function ($q)
{
$q->on('trader_job_payment.payable_id', '=', 'customer_class_bookings.id');
$q->on('trader_job_payment.payable_type', '=', \DB::raw("'" . CustomerClassBooking::class . "'"));
})
->select(
\DB::raw("SUM(IF (customer_class_bookings.status = 'Completed', customer_class_bookings.quantity, 0)) AS completed"),
\DB::raw("SUM(IF (trader_job_payment.status = 'DISBURSED' , customer_class_bookings.total, 0)) AS earning"), //problem lies here
\DB::raw("SUM(IF (customer_class_bookings.status = 'Accepted-Paid' , customer_class_bookings.quantity, 0)) AS pending"),
\DB::raw("SUM(IF (customer_class_bookings.status IN ('Cancelled', 'Disputed') , customer_class_bookings.quantity, 0)) AS not_completed")
)
->first();
return $earnings;
Все работает нормально, пока мы не сопоставим что-то с trader_job_payment
таблица, как в earnings
секции
\DB::raw("SUM(IF (trader_job_payment.status = 'DISBURSED' , customer_class_bookings.total, 0)) AS earning"),
, где она не дает идеальных результатов, так как условие IF
всегда возвращает ложь в этом случае. Кто-нибудь может сказать мне, что может быть проблема здесь?
Заработать результат без левого соединения
$earning_bookings = CustomerClassBooking::whereHas('payment', function($query){
$query->where('status','DISBURSED');
})->where('trader_user_id',$user_id)->sum('total');