model->with($relationships)->select($select); if (isset($data['status'])) { $model = $model->where("status", $data['status']); } if (isset($data['module_network_id']) && $data['module_network_id']) { $model = $model->where("module_network_id", $data['module_network_id']); } if (count($withCount) > 0) { $model = $model->withCount($withCount); } if (isset($data['page'])) { return $model->paginate(15); } return $model->get(); } public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = []) { $model = $this->model; if (count($relationships) > 0) { foreach ($relationships as $relationship) { if (method_exists($model, $relationship)) { $model = $model->with($relationship); } } } // Xử lý withCount if (count($withCount) > 0) { foreach ($withCount as $key => $items) { if (is_array($items)) { foreach ($items as $alias => $item) { $model = $model->withCount([ $key . ' as ' . $alias => function ($query) use ($item) { $query->where($item['key'], $item['condition'], $item['value']); } ]); } } else { $model = $model->withCount($key); } } } // Xử lý withSum if (count($withSum) > 0) { foreach ($withSum as $key => $items) { if (is_array($items)) { foreach ($items as $alias => $item) { $model = $model->withSum([ $key . ' as ' . $alias => function ($query) use ($item) { $query->where($item['key'], $item['condition'], $item['value']); } ], $item['sum']); } } else { $model = $model->withSum($key); } } } return $model->find($id); } public function updateBy(array $condistions = [], array $data = []) { $model = $this->model; if (count($condistions) > 0) { foreach ($condistions as $key => $value) { $model = $model->where($key, $value); } $model = $model->first(); if ($model) { return $model->update($data); } return false; } return false; } public function findBy(string $key = '', string|int|null $value = '') { if (empty($key) || empty($value)) { return false; } return $this->model->isFree()->where($key, $value)->first(); } }