Laravel Sorting via url
I am trying to add simple sorting to my index pages, but I am coming into conflict when I try to include search results or add an additional query to the URL. Here is what I currently have:
Controller
// Search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
// Sort default
if ($request->sort == null) {
$sort = 'asc';
} else {
$sort = $request->sort;
}
$doctors = Doctor::orderBy('last_name', $sort)
->where('first_name', 'LIKE', '%' . $search . '%')
->orWhere('last_name', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->orWhere('npi', 'LIKE', '%' . $search . '%')
->orWhere('license', 'LIKE', '%' . $search . '%')
->orWhere('dea', 'LIKE', '%' . $search . '%')
->paginate(10);
return view('doctors.index')->with('doctors', $doctors);
Blade
{{ Form::open(['method' => 'GET']) }}
{{ Form::text('search', null, ['class' => 'form-control form-control-sm', 'placeholder' => 'Search Doctors...']) }}
{{Form::submit('Search', ['class' => 'btn btn-primary btn-sm'])}}
{{ Form::close() }}
@if(Request::input('sort', 'asc') == 'asc')
<a href="?sort=desc">Name</a>
@else
<a href="?sort=asc">Name</a>
@endif
Now, independently, the ?search= and the ?sort= will work. But if I try to search for something, then sort the results, it clears out the search request. I also have other columns such as License#, Address, etc that I want to add so that people can sort by more than just the Name. However, when I change the requests to add a ?sortBy= such as<a href="?sortBy=name?sort=desc">Name</a>, the sort direction no longer works.
php laravel
add a comment |
I am trying to add simple sorting to my index pages, but I am coming into conflict when I try to include search results or add an additional query to the URL. Here is what I currently have:
Controller
// Search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
// Sort default
if ($request->sort == null) {
$sort = 'asc';
} else {
$sort = $request->sort;
}
$doctors = Doctor::orderBy('last_name', $sort)
->where('first_name', 'LIKE', '%' . $search . '%')
->orWhere('last_name', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->orWhere('npi', 'LIKE', '%' . $search . '%')
->orWhere('license', 'LIKE', '%' . $search . '%')
->orWhere('dea', 'LIKE', '%' . $search . '%')
->paginate(10);
return view('doctors.index')->with('doctors', $doctors);
Blade
{{ Form::open(['method' => 'GET']) }}
{{ Form::text('search', null, ['class' => 'form-control form-control-sm', 'placeholder' => 'Search Doctors...']) }}
{{Form::submit('Search', ['class' => 'btn btn-primary btn-sm'])}}
{{ Form::close() }}
@if(Request::input('sort', 'asc') == 'asc')
<a href="?sort=desc">Name</a>
@else
<a href="?sort=asc">Name</a>
@endif
Now, independently, the ?search= and the ?sort= will work. But if I try to search for something, then sort the results, it clears out the search request. I also have other columns such as License#, Address, etc that I want to add so that people can sort by more than just the Name. However, when I change the requests to add a ?sortBy= such as<a href="?sortBy=name?sort=desc">Name</a>, the sort direction no longer works.
php laravel
add a comment |
I am trying to add simple sorting to my index pages, but I am coming into conflict when I try to include search results or add an additional query to the URL. Here is what I currently have:
Controller
// Search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
// Sort default
if ($request->sort == null) {
$sort = 'asc';
} else {
$sort = $request->sort;
}
$doctors = Doctor::orderBy('last_name', $sort)
->where('first_name', 'LIKE', '%' . $search . '%')
->orWhere('last_name', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->orWhere('npi', 'LIKE', '%' . $search . '%')
->orWhere('license', 'LIKE', '%' . $search . '%')
->orWhere('dea', 'LIKE', '%' . $search . '%')
->paginate(10);
return view('doctors.index')->with('doctors', $doctors);
Blade
{{ Form::open(['method' => 'GET']) }}
{{ Form::text('search', null, ['class' => 'form-control form-control-sm', 'placeholder' => 'Search Doctors...']) }}
{{Form::submit('Search', ['class' => 'btn btn-primary btn-sm'])}}
{{ Form::close() }}
@if(Request::input('sort', 'asc') == 'asc')
<a href="?sort=desc">Name</a>
@else
<a href="?sort=asc">Name</a>
@endif
Now, independently, the ?search= and the ?sort= will work. But if I try to search for something, then sort the results, it clears out the search request. I also have other columns such as License#, Address, etc that I want to add so that people can sort by more than just the Name. However, when I change the requests to add a ?sortBy= such as<a href="?sortBy=name?sort=desc">Name</a>, the sort direction no longer works.
php laravel
I am trying to add simple sorting to my index pages, but I am coming into conflict when I try to include search results or add an additional query to the URL. Here is what I currently have:
Controller
// Search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
// Sort default
if ($request->sort == null) {
$sort = 'asc';
} else {
$sort = $request->sort;
}
$doctors = Doctor::orderBy('last_name', $sort)
->where('first_name', 'LIKE', '%' . $search . '%')
->orWhere('last_name', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->orWhere('npi', 'LIKE', '%' . $search . '%')
->orWhere('license', 'LIKE', '%' . $search . '%')
->orWhere('dea', 'LIKE', '%' . $search . '%')
->paginate(10);
return view('doctors.index')->with('doctors', $doctors);
Blade
{{ Form::open(['method' => 'GET']) }}
{{ Form::text('search', null, ['class' => 'form-control form-control-sm', 'placeholder' => 'Search Doctors...']) }}
{{Form::submit('Search', ['class' => 'btn btn-primary btn-sm'])}}
{{ Form::close() }}
@if(Request::input('sort', 'asc') == 'asc')
<a href="?sort=desc">Name</a>
@else
<a href="?sort=asc">Name</a>
@endif
Now, independently, the ?search= and the ?sort= will work. But if I try to search for something, then sort the results, it clears out the search request. I also have other columns such as License#, Address, etc that I want to add so that people can sort by more than just the Name. However, when I change the requests to add a ?sortBy= such as<a href="?sortBy=name?sort=desc">Name</a>, the sort direction no longer works.
php laravel
php laravel
asked Nov 15 '18 at 20:40
XerakonXerakon
8010
8010
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The URL query params need to be separated by & not ? like so:
<a href="?sortBy=name&sort=desc">Name</a>
Thank you! I've changed my if statements to be more similar to<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.
– Xerakon
Nov 15 '18 at 20:59
add a comment |
You will need to append the query to $doctors:
$doctors->addQuery('sort', request()->get('sort'))->addQuery('sortBy', request()->get('sortBy'))
Edit:
It appears the api changed for later versions, so use appends:
->appends(['sort' => request()->get('sort'), 'sortBy' => request()->get('sortBy')])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327580%2flaravel-sorting-via-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The URL query params need to be separated by & not ? like so:
<a href="?sortBy=name&sort=desc">Name</a>
Thank you! I've changed my if statements to be more similar to<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.
– Xerakon
Nov 15 '18 at 20:59
add a comment |
The URL query params need to be separated by & not ? like so:
<a href="?sortBy=name&sort=desc">Name</a>
Thank you! I've changed my if statements to be more similar to<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.
– Xerakon
Nov 15 '18 at 20:59
add a comment |
The URL query params need to be separated by & not ? like so:
<a href="?sortBy=name&sort=desc">Name</a>
The URL query params need to be separated by & not ? like so:
<a href="?sortBy=name&sort=desc">Name</a>
answered Nov 15 '18 at 20:48
ParasParas
5,8901035
5,8901035
Thank you! I've changed my if statements to be more similar to<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.
– Xerakon
Nov 15 '18 at 20:59
add a comment |
Thank you! I've changed my if statements to be more similar to<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.
– Xerakon
Nov 15 '18 at 20:59
Thank you! I've changed my if statements to be more similar to
<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.– Xerakon
Nov 15 '18 at 20:59
Thank you! I've changed my if statements to be more similar to
<a href="?sortBy=last_name&sort=desc">Name</a>, which covers the sortBy and the sorting direction. However, I am still unable to figure out the search functionality.– Xerakon
Nov 15 '18 at 20:59
add a comment |
You will need to append the query to $doctors:
$doctors->addQuery('sort', request()->get('sort'))->addQuery('sortBy', request()->get('sortBy'))
Edit:
It appears the api changed for later versions, so use appends:
->appends(['sort' => request()->get('sort'), 'sortBy' => request()->get('sortBy')])
add a comment |
You will need to append the query to $doctors:
$doctors->addQuery('sort', request()->get('sort'))->addQuery('sortBy', request()->get('sortBy'))
Edit:
It appears the api changed for later versions, so use appends:
->appends(['sort' => request()->get('sort'), 'sortBy' => request()->get('sortBy')])
add a comment |
You will need to append the query to $doctors:
$doctors->addQuery('sort', request()->get('sort'))->addQuery('sortBy', request()->get('sortBy'))
Edit:
It appears the api changed for later versions, so use appends:
->appends(['sort' => request()->get('sort'), 'sortBy' => request()->get('sortBy')])
You will need to append the query to $doctors:
$doctors->addQuery('sort', request()->get('sort'))->addQuery('sortBy', request()->get('sortBy'))
Edit:
It appears the api changed for later versions, so use appends:
->appends(['sort' => request()->get('sort'), 'sortBy' => request()->get('sortBy')])
edited Nov 15 '18 at 21:22
answered Nov 15 '18 at 21:00
adamadam
927811
927811
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327580%2flaravel-sorting-via-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown