From fe707514b33941a91cdbb300b0eb108d5a2a9b85 Mon Sep 17 00:00:00 2001 From: Much Yusron Arif <3467343+yusronarif@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:59:56 +0700 Subject: [PATCH 1/2] add table alias property on model --- src/Database/Eloquent/Model.php | 55 +++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/src/Database/Eloquent/Model.php b/src/Database/Eloquent/Model.php index 23f7148..f098cef 100644 --- a/src/Database/Eloquent/Model.php +++ b/src/Database/Eloquent/Model.php @@ -15,6 +15,13 @@ class Model extends BaseModel { use HasTimestamps, GeneralScope; + /** + * Set table alias on query builder + * + * @var string|null + */ + public static string|null $tableAlias = null; + /** * The list of table wich include with schema. */ @@ -37,13 +44,7 @@ class Model extends BaseModel */ protected bool $useTryOnEnumCast = true; - /** - * Create a new Eloquent model instance. - * - * @param array $attributes - * - * @return void - */ + /** @inheritdoc */ public function __construct(array $attributes = []) { parent::__construct($attributes); @@ -52,11 +53,7 @@ public function __construct(array $attributes = []) $this->fullnameTable['self'] = "{$schema}.{$this->table}"; } - /** - * Get the value indicating whether the IDs are incrementing. - * - * @return bool - */ + /** @inheritdoc */ public function getIncrementing() { if (in_array(strtolower($this->getKeyType()), ['string', 'uuid'])) { @@ -67,12 +64,38 @@ public function getIncrementing() } /** - * Perform a model insert operation. - * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param array $data * - * @return bool + * @return array */ + public static function getFillableAttribute(array $data): array + { + $fillable = (new static)->getFillable(); + + return Arr::only($data, Arr::flatten($fillable)); + } + + /** @inheritdoc */ + public function newQuery() + { + $query = parent::newQuery(); + + if (static::$tableAlias) { + return $query->from($this->getTable(), static::$tableAlias); + } + return $query; + } + + /** @inheritdoc */ + public function qualifyColumn($column) + { + if (static::$tableAlias) { + $column = static::$tableAlias . '.' . $column; + } + return parent::qualifyColumn($column); + } + + /** @inheritdoc */ protected function performInsert(Builder $query) { if (in_array($keyType = strtolower($this->getKeyType()), ['string', 'uuid', 'ulid'])) { From 168b7195edb0ff94dba3258b715c599e7990354c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 9 Nov 2023 05:00:45 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Database/Eloquent/Model.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Database/Eloquent/Model.php b/src/Database/Eloquent/Model.php index f098cef..6e58c0e 100644 --- a/src/Database/Eloquent/Model.php +++ b/src/Database/Eloquent/Model.php @@ -16,7 +16,7 @@ class Model extends BaseModel use HasTimestamps, GeneralScope; /** - * Set table alias on query builder + * Set table alias on query builder. * * @var string|null */ @@ -83,6 +83,7 @@ public function newQuery() if (static::$tableAlias) { return $query->from($this->getTable(), static::$tableAlias); } + return $query; } @@ -90,8 +91,9 @@ public function newQuery() public function qualifyColumn($column) { if (static::$tableAlias) { - $column = static::$tableAlias . '.' . $column; + $column = static::$tableAlias.'.'.$column; } + return parent::qualifyColumn($column); }