Skip to content

Commit

Permalink
Add random order option
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissawyerfan4 committed Apr 3, 2023
1 parent 5a62672 commit 48c84aa
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 4 deletions.
9 changes: 6 additions & 3 deletions app/Http/Controllers/Models/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ public function index(Request $request): View

$links = Link::byUser()
->with('tags')
->orderBy($orderBy, $orderDir)
->paginate(getPaginationLimit());
->orderBy($orderBy, $orderDir);

if ($orderBy == 'random') {
$links->inRandomOrder();
}

return view('models.links.index', [
'links' => $links,
'links' => $links->paginate(getPaginationLimit()),
'route' => $request->getBaseUrl(),
'orderBy' => $orderBy,
'orderDir' => $orderDir,
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/Traits/SearchesLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ protected function buildDatabaseQuery(SearchRequest $request): Builder

// Order the results if applicable
if ($this->searchOrderBy = $request->input('order_by', $this->orderByOptions[0])) {
$search->orderBy(...explode(':', $this->searchOrderBy));
if ($this->searchOrderBy == 'random') {
$search->inRandomOrder();
} else {
$search->orderBy(...explode(':', $this->searchOrderBy));
}
}

// Return the query builder itself
Expand Down
1 change: 1 addition & 0 deletions lang/de_DE/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'order_by.created_at:desc' => 'Erstellungsdatum absteigend',
'order_by.number_links:asc' => 'Anzahl der Links aufsteigend',
'order_by.number_links:desc' => 'Anzahl der Links absteigend',
'order_by.random' => 'Zufällig',

'no_results' => 'Keine Ergebnisse gefunden.',

Expand Down
1 change: 1 addition & 0 deletions lang/en_US/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'order_by.created_at:desc' => 'Creation Date descending',
'order_by.number_links:asc' => 'Number of Links ascending',
'order_by.number_links:desc' => 'Number of Links descending',
'order_by.random' => 'Random',

'no_results' => 'No results found.',

Expand Down
1 change: 1 addition & 0 deletions lang/fr_FR/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'order_by.created_at:desc' => 'Date de Création descendante',
'order_by.number_links:asc' => 'Nombre de Liens ascendants',
'order_by.number_links:desc' => 'Nombre de Liens descendants',
'order_by.random' => 'Aléatoire',

'no_results' => 'Aucun résultat trouvé.',

Expand Down
4 changes: 4 additions & 0 deletions resources/views/app/search/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class="form-check-input"
@lang('search.order_by.' . $order_by)
</option>
@endforeach
<option value="random"
@if($query_settings['order_by'] == 'random') selected @endif>
@lang('search.order_by.random')
</option>
</select>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ class="btn btn-sm btn-outline-primary dropdown-toggle dropdown-toggle-split"
href="{{ route($baseRoute, ['orderBy' => 'title', 'orderDir' => 'desc']) }}">
@lang('search.order_by.title:desc')
</a>
<a class="dropdown-item small"
href="{{ route($baseRoute, ['orderBy' => 'random']) }}">
@lang('search.order_by.random')
</a>
</div>

0 comments on commit 48c84aa

Please sign in to comment.