@php // Preload status codes with leads and dynamically join only the necessary tables based on conditions $leadsQuery = DB::table('lead_sales') ->leftJoin('status_codes', 'lead_sales.status', '=', 'status_codes.status_code') ->select( 'lead_sales.*', 'status_codes.status_name', 'elife_telesales_plans.plan_name as elife_telesales_plan_name', 'home_wifi_plans.plan_name as home_wifi_plan_name', 'postpaid_mnp_plans.plan_name as postpaid_mnp_plan_name', 'postpaid_new_plans.plan_name as postpaid_new_plan_name', ); // Dynamic joins based on conditions with unique aliases $leadsQuery->when( fn($query) => $query ->where('lead_sales.sim_type', 'Elife') ->whereIn('lead_sales.sale_type', ['Elife Telesales', 'Elife DTD']), fn($query) => $query->leftJoin( 'elife_plans as elife_telesales_plans', 'lead_sales.select_plan', '=', 'elife_telesales_plans.id', ), ); $leadsQuery->when( fn($query) => $query->whereIn('lead_sales.sim_type', ['HomeWifiTelesales', 'HomeWifiDTD']), fn($query) => $query->leftJoin( 'elife_plans as home_wifi_plans', 'lead_sales.select_plan', '=', 'home_wifi_plans.id', ), ); $leadsQuery->when( fn($query) => $query ->where('lead_sales.sim_type', 'MNP') ->whereIn('lead_sales.sale_type', ['Postpaid DTD', 'Postpaid Telesales']), fn($query) => $query->leftJoin( 'plans as postpaid_mnp_plans', 'lead_sales.select_plan', '=', 'postpaid_mnp_plans.id', ), ); $leadsQuery->when( fn($query) => $query->where('lead_sales.sim_type', 'New')->where('lead_sales.sale_type', 'Postpaid Telesales'), fn($query) => $query->leftJoin( 'plans as postpaid_new_plans', 'lead_sales.select_plan', '=', 'postpaid_new_plans.id', ), ); // Execute the query and get results $leads = $leadsQuery->get(); // Post-process to only show the relevant plan name based on sim_type $leads->transform(function ($lead) { switch ($lead->sim_type) { case 'Elife': $lead->display_plan_name = $lead->elife_telesales_plan_name; break; case 'HomeWifiTelesales': case 'HomeWifiDTD': $lead->display_plan_name = $lead->home_wifi_plan_name; break; case 'MNP': $lead->display_plan_name = $lead->postpaid_mnp_plan_name; break; case 'New': $lead->display_plan_name = $lead->postpaid_new_plan_name; break; default: $lead->display_plan_name = null; break; } return $lead; }); // dd($leads); @endphp @foreach ($leads as $lead_sale) @endforeach
Customer Name Gender Emirates Sim Type Select Plan Lead No Language Status Created At Updated At Lead Type Channel Type Sale Type Sr Number Request Number Lead Refrence Number Verified Status SR Status
{{ $lead_sale->customer_name }} {{ $lead_sale->gender }} {{ $lead_sale->emirates }} {{ $lead_sale->sim_type }} {{ $lead_sale->display_plan_name ?? 'N/A' }} {{ $lead_sale->lead_no }} {{ $lead_sale->language }} {{ $lead_sale->status_name ?? 'N/A' }} {{ $lead_sale->created_at }} {{ $lead_sale->updated_at }} {{ $lead_sale->lead_type }} {{ $lead_sale->channel_type }} {{ $lead_sale->sale_type }} {{ $lead_sale->sr_number }} {{ $lead_sale->request_number }} {{ $lead_sale->lead_refrence_number }} {{ $lead_sale->verified_status }} {{ $lead_sale->sr_status }}