columnName = $column; $this->as = $column; } else { $ret = ""; foreach ($column as $c) { if ($ret !== "") { $ret .= "."; } $ret .= sprintf('"%s"', $c); } $this->columnName = $ret; $this->as = Arr::last($column); } if ($as !== null) { $this->as = $as; } } public function as(string $as): static { $this->as = $as; return $this; } public function nullValue(string|int|null $nullValue): static { $this->nullValue = $nullValue; return $this; } public function build() { $query = $this->columnName; $needRaw = true; if ($this->nullValue !== null) { if (is_string($this->nullValue)) { $query = sprintf("COALESCE(%s, '%s')", $query, $this->nullValue); $needRaw = true; } else if (is_int($this->nullValue)) { $query = sprintf("COALESCE(%s, %d)", $query, $this->nullValue); $needRaw = true; } } if ($this->as !== null) { $query = sprintf('%s as %s', $query, $this->as); } return $needRaw ? DB::raw($query) : $query; } }