Mysql / Design / Show Status based on next level
Next Level Status
-
Option 1
DB structure
CREATE TABLE `lead_statuses` ( `id` bigint(20) UNSIGNED NOT NULL, `company_id` int(11) DEFAULT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `identifier` char(25) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `stage` varchar(1000) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `status` enum('1','0') COLLATE utf8mb4_unicode_ci DEFAULT '1', `priority` int(11) DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `created_by` bigint(20) DEFAULT NULL, `updated_by` bigint(20) DEFAULT NULL, `deleted_by` bigint(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -- Dumping data for table `lead_statuses` -- INSERT INTO `lead_statuses` (`id`, `company_id`, `name`, `identifier`, `stage`, `status`, `priority`, `deleted_at`, `created_at`, `updated_at`, `created_by`, `updated_by`, `deleted_by`) VALUES (2, 1, 'Enquiry', 'enquiry', 'stage11', '1', 1, NULL, '2022-01-24 13:15:57', '2022-01-24 13:15:57', NULL, NULL, NULL), (3, 1, 'Boq / Site Survey', 'survey', 'stage12,stage18', '1', 2, NULL, '2022-01-24 13:16:17', '2022-01-24 13:16:17', NULL, NULL, NULL), (4, 1, 'BoQ', 'boq', 'stage12,stage18', '1', 3, NULL, '2022-01-24 13:16:25', '2022-01-24 13:16:25', NULL, NULL, NULL), (5, 1, 'Commercial', 'commercial', 'stage13,stage18', '1', 4, NULL, '2022-01-24 13:16:37', '2022-01-24 13:16:37', NULL, NULL, NULL), (6, 1, 'Negotiations', 'negotiations', 'stage14,stage18', '1', 5, NULL, '2022-01-24 13:17:02', '2022-01-24 13:17:02', NULL, NULL, NULL), (7, 1, 'Won', 'win', 'stage15,stage18', '1', 6, NULL, '2022-01-24 13:17:35', '2022-01-24 13:17:35', NULL, NULL, NULL), (8, 1, 'Failed', 'failed', 'stage15,stage18', '1', 7, NULL, '2022-01-24 13:18:00', '2022-01-24 13:18:00', NULL, NULL, NULL), (9, 1, 'Expired', 'expired', 'stage17', '1', 8, NULL, '2022-01-24 13:18:23', '2022-01-24 13:18:23', NULL, NULL, NULL); in controller
public function followup_form($id){ $lead=Lead::find($id); switch($lead->status){ case 'follwoup': $input['stage']='stage12'; break; case 'commercial': $input['stage']='stage13'; break; default: $input['stage']='stage11'; break; } $query = LeadStatus::query(); if(isset($input['stage'])){ $query=$query->where('stage','like', '%' . $input['stage']. '%'); } $this->data['statusData']=$query->get(); // pass to view form } -
Option 2
1. status_masters
id title status_identifier priority 2. status_level_relations
id status_id parent_id -
Create a new status
$object = Status::create($input); $newStatus=Status::where('identifier','new')->first(); if(empty($newStatus)){ $newStatus=Status::create( [ 'identifier' =>'new', 'name' => 'New' ] ); } StatusLevelRelation::create([ 'parent_id'=>$newStatus->id, 'status_id'=>$object->id ]); -
Display level based status
List first level
List Next level$query = LeadStatus::join('lead_status_relations','lead_status_relations.parent_id','lead_statuses.id') ->join('lead_statuses as c','c.id','lead_status_relations.status_id'); $query = $query->select('c.id','c.name') ->where('lead_statuses.company_id',$user->company_id) ->where('lead_statuses.identifier','new') ->whereNull('lead_statuses.deleted_by') ->orderBy('lead_statuses.name'); $responseData=$query->get(); $query = LeadStatus::join('lead_statuses as c','c.id','lead_status_relations.status_id'); $query = $query->select('c.id','c.name') ->where('lead_statuses.company_id',$user->company_id) ->where('lead_status_relations.parent_id',$input['status_id']) ->whereNull('lead_statuses.deleted_by') ->orderBy('lead_statuses.name'); $responseData=$query->get();