diff --git a/Modules/Agents/Entities/Customer.php b/Modules/Agents/Entities/Customer.php index f04dfc5..bc2f795 100755 --- a/Modules/Agents/Entities/Customer.php +++ b/Modules/Agents/Entities/Customer.php @@ -11,7 +11,7 @@ class Customer extends Model protected $table = 'se_customers'; protected $fillable = ['id', 'name', 'phone', 'email', 'grade', 'msg', 'agent_id', 'created_at', 'created_by', 'updated_at', 'updated_by', 'deleted_at']; protected $dates = ['deleted_at']; - + // protected static function newFactory() // { // return \Modules\Agents\Database\factories\CustomerFactory::new(); @@ -23,7 +23,8 @@ class Customer extends Model * @param mixed $phone * @return mixed */ - public function scopeByPhone($query, $phone){ + public function scopeByPhone($query, $phone) + { return $query->where('phone', $phone); } /** @@ -32,7 +33,8 @@ class Customer extends Model * @param mixed $agentId * @return mixed */ - public function scopeNotAgent($query, $agentId){ + public function scopeNotAgent($query, $agentId) + { return $query->where('agent_id', '<>', $agentId); } /** @@ -41,7 +43,16 @@ class Customer extends Model * @param mixed $agentId * @return mixed * **/ - public function scopeInAgent($query, $agentId){ + public function scopeInAgent($query, $agentId) + { return $query->where('agent_id', '=', $agentId); } + public function scopeSearch($query, $searchKey) + { + return $query->where(function ($query) use ($searchKey) { + $query->where('name', 'LIKE', '%' . $searchKey . '%') + ->orWhere('phone', 'LIKE', '%' . $searchKey . '%') + ->orWhere('email', 'LIKE', '%' . $searchKey . '%'); + }); + } } diff --git a/Modules/Agents/Services/AgentService.php b/Modules/Agents/Services/AgentService.php index 1105e78..e490a4e 100644 --- a/Modules/Agents/Services/AgentService.php +++ b/Modules/Agents/Services/AgentService.php @@ -95,7 +95,7 @@ class AgentService return ['status' => false, 'msg' => $this->getMessageReturn('no_data_agent')]; } $agentId = $myAgent->agent_id; - $queryAgent = $this->modelCustomer->where('agent_id', $agentId); + $queryAgent = $this->modelCustomer->where('agent_id', '=',$agentId); if (!empty($input['start_date']) && !empty($input['end_date'])) { $startDate = Carbon::createFromFormat('Y-m-d', $input['start_date']); $endDate = Carbon::createFromFormat('Y-m-d', $input['end_date'])->addDay(); @@ -103,10 +103,7 @@ class AgentService $queryAgent->whereBetween('created_at', [$startDate->toDateString(), $endDate->toDateString()]); } if (!empty($input['keyword'])) { - $queryAgent->where('name', 'LIKE', '%' . $input['keyword'] . '%') - ->orWhere('phone', 'LIKE', '%' . $input['keyword'] . '%') - ->orWhere('email', 'LIKE', '%' . $input['keyword'] . '%'); - ; + $queryAgent->search($input['keyword']); } // dd($queryAgent->toSql(),$queryAgent->getBindings()); $listCustomer = $queryAgent->orderBy('created_at', 'desc')->get(); @@ -119,7 +116,8 @@ class AgentService 'email' => $listC->email ?? '', 'grade' => $listC->grade ?? '', 'date_receive' => $listC->created_at ?? '', - 'message' => $listC->msg ?? '' + 'message' => $listC->msg ?? '', + 'agent_id' => $listC->agent_id ?? '' ]; } }