Skip to content

Commit

Permalink
Merge pull request #2 from ajcastro/1.2
Browse files Browse the repository at this point in the history
Add helper methods isColumnValid() and getTableColumns()
  • Loading branch information
ajcastro authored Apr 12, 2019
2 parents 124f067 + d99d63a commit 2c8aacd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ class PostsController
{
public function index()
{
return Post::sortByRelevance(!request()->bool('sort_by'))->search(request('search'))
->when($sortColumn = request('sort_by'), function ($query) use ($sortColumn) {
return Post::sortByRelevance(!request()->bool('sort_by'))
->search(request('search'))
->when(Post::isColumnValid($sortColumn = request('sort_by')), function ($query) use ($sortColumn) {
$query->orderBy(
\DB::raw($this->model->searchQuery()->getColumn($sortColumn) ?? $sortColumn),
\DB::raw(Post::searchQuery()->getColumn($sortColumn) ?? $sortColumn),
request()->bool('descending') ? 'desc' : 'asc'
);
})
Expand Down Expand Up @@ -341,6 +342,25 @@ $results = [
];
```

## Helper methods available on model

### isColumnValid

- Identifies if the column is a valid column, either a regular table column or derived column.
- Useful for checking valid columns to avoid sql injection especially in `orderBy` query.

```php
Post::isColumnValid(request('sort_by'));
```

### getTableColumns

- Get the table columns.

```php
Post::getTableColumns();
```

## Credits

- Ray Anthony Madrona [@raymadrona](https://github.com/raymadrona), for the tips on using MySQL `LOCATE()` for sort relevance.
Expand Down
42 changes: 41 additions & 1 deletion src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AjCastro\Searchable;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Schema;
use AjCastro\Searchable\Search\SublimeSearch;

Expand All @@ -26,13 +27,52 @@ public function searchableColumns()
return $this->searchable['columns'];
}

if (!array_key_exists($table = $this->getTable(), static::$allSearchableColumns)) {
return static::getTableColumns($this->getTable());
}

/**
* Get table columns.
*
* @param string $table
* @return array
*/
public static function getTableColumns($table = null)
{
$table = $table ?? (new static)->getTable();

if (!Arr::has(static::$allSearchableColumns, $table)) {
static::$allSearchableColumns[$table] = Schema::getColumnListing($table);
}

return static::$allSearchableColumns[$table];
}

/**
* Identifies if the column is a valid column, either a regular table column or derived column.
* Useful for checking valid columns to avoid sql injection especially in orderBy query.
*
* @param string $column
* @return boolean
*/
public static function isColumnValid($column)
{
$model = new static;
$searchableColumns = $model->searchableColumns();

// Derived columns are a key in searchableColumns.
if (array_key_exists($column, $searchableColumns)) {
return true;
}

// Regular table column can be included in the searchableColumns.
if (in_array($column, $searchableColumns)) {
return true;
}

// Regular table column from the table
return in_array($column, static::getTableColumns($model->getTable()));
}

/**
* Return the searchable joins for the search query.
*
Expand Down

0 comments on commit 2c8aacd

Please sign in to comment.