Skip to content

Commit

Permalink
Added ` escapes around dynamic field names for db::raw calls
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin.schmick committed Jul 22, 2022
1 parent 2cb3717 commit 63d080e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/BaseFeatures/Filters/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,9 @@ public function toArray() : array
'key' => $this->key(),
];
}

protected static function escapeFieldName(string $field) : string
{
return '`' . implode('`.`', explode('.', $field)) . '`';
}
}
2 changes: 1 addition & 1 deletion src/BaseFeatures/Filters/ContainsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function apply(Builder $builder, array $options = []) : Builder
*/
public static function build(Builder $builder, mixed $field, mixed $value, string $action = 'where') : Builder
{
return $builder->$action(DB::raw('COALESCE(' . $field . ", '')"), 'like', '%' . $value . '%');
return $builder->$action(DB::raw('COALESCE(' . self::escapeFieldName($field) . ", '')"), 'like', '%' . $value . '%');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/BaseFeatures/Filters/DoesNotContainFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function apply(Builder $builder, array $options = []) : Builder
*/
public static function build(Builder $builder, mixed $field, mixed $value, string $action = 'where') : Builder
{
return $builder->$action(DB::raw('COALESCE(' . $field . ", '')"), 'not like', '%' . $value . '%');
return $builder->$action(DB::raw('COALESCE(' . self::escapeFieldName($field) . ", '')"), 'not like', '%' . $value . '%');
}

/**
Expand Down

0 comments on commit 63d080e

Please sign in to comment.