model->with($relationships); // Handle customer_id filtering through invoice relationship if ($userId > 0) { $query = $query->whereHas('invoice', function ($subQuery) use ($userId) { $subQuery->where('customer_id', $userId); }); } // Handle additional data conditions, but skip customer_id if it's trying to query invoice_item directly if (!empty($data) && isset($data['key']) && $data['key'] !== 'customer_id') { $query = $query->where($data['key'], $data['condition'], $data['value']); } return $query->first(); } public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*']) { $model = $this->model->with($relationships)->select($select); if (isset($data['invoice_type'])) { $model = $model->where("invoice_type", $data['invoice_type']); } if (isset($data['parent_id'])) { $model = $model->where("parent_id", $data['parent_id']); } // if (isset($data['customer_id'])) { // $model = $model->where("customer_id", $data['customer_id']); // } if (isset($data['invoice_id'])) { $model = $model->where("invoice_id", $data['invoice_id']); } if (count($withCount) > 0) { $model = $model->withCount($withCount); } // if (isset($data['type_id'])) { // $model = $model->where(function ($query) use ($data) { // if (isset($data['type_id']['condition'])) { // switch (strtoupper($data['type_id']['condition'])) { // case "IN": // $query->whereIn($data['type_id']['key'], $data['type_id']['value']); // break; // case "NotIn": // $query->whereNotIn($data['type_id']['key'], $data['type_id']['value']); // break; // default: // $query->where($data['type_id']['key'], $data['type_id']['condition'], $data['type_id']['value']); // break; // } // } else { // $query->where($data['type_id']['key'], $data['type_id']['value']); // } // }); // } if (isset($data['relations'])) { foreach ($data['relations'] as $key => $value) { $model = $model->whereHas($key, function ($query) use ($value) { if (isset($value['condition'])) { switch (strtoupper($value['condition'])) { case "IN": $query->whereIn($value['key'], $value['value']); break; case "NotIn": $query->whereNotIn($value['key'], $value['value']); break; default: $query->where($value['key'], $value['condition'], $value['value']); break; } } else { $query->where($value['key'], $value['value']); } }); } } if (isset($data['limit']) && $data['limit']) { return $model->latest('id')->take($data['limit'])->get(); } 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); } }