Laravel Limit Results by Many-Many Relationship
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->paginate(10);
This worked just fine. However, I recently added search functionality that looks like this:
// search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $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);
Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search
return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?
laravel
add a comment |
I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->paginate(10);
This worked just fine. However, I recently added search functionality that looks like this:
// search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $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);
Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search
return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?
laravel
add a comment |
I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->paginate(10);
This worked just fine. However, I recently added search functionality that looks like this:
// search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $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);
Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search
return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?
laravel
I am trying to set up a view specifically for a user that limits their data to display only doctors that were assigned to them via a many-many relationship. Here is the original code:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->paginate(10);
This worked just fine. However, I recently added search functionality that looks like this:
// search default
if ($request->search == null) {
$search = '';
} else {
$search = $request->search;
}
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $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);
Now when I look at it from the rep's point of view, it shows all of the doctors in the database. I'm assuming that making the $search
return '', it is overriding the User->doctors() request. How can I keep the search functionality but make sure and limit the doctors to those that are attributed only to the user?
laravel
laravel
asked Nov 16 '18 at 15:52
XerakonXerakon
8710
8710
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You need to group the orWhere
clauses, otherwise your getting all doctors associated with the user and where the first_name
is like $search
OR where the last_name
is like $search
on all doctors.
try this:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->where(function ($query) use ($search) {
return $query->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);
add a comment |
You need to use when and group where
$search = $request->input('search');
$doctors = User::find(Auth::user()->id)
->doctors()
->when($search, function($query) use ($search){
$query->where(function ($query) use ($search) {
$query
->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 . '%');
})
})
->orderBy('...')
->paginate(10);
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
@IndianCoding To make sure I am fully understanding this code, thewhen
clause will essentially replace myif
check to see if a search query has been submitted?
– Xerakon
Nov 16 '18 at 17:12
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
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%2f53341289%2flaravel-limit-results-by-many-many-relationship%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
You need to group the orWhere
clauses, otherwise your getting all doctors associated with the user and where the first_name
is like $search
OR where the last_name
is like $search
on all doctors.
try this:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->where(function ($query) use ($search) {
return $query->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);
add a comment |
You need to group the orWhere
clauses, otherwise your getting all doctors associated with the user and where the first_name
is like $search
OR where the last_name
is like $search
on all doctors.
try this:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->where(function ($query) use ($search) {
return $query->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);
add a comment |
You need to group the orWhere
clauses, otherwise your getting all doctors associated with the user and where the first_name
is like $search
OR where the last_name
is like $search
on all doctors.
try this:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->where(function ($query) use ($search) {
return $query->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);
You need to group the orWhere
clauses, otherwise your getting all doctors associated with the user and where the first_name
is like $search
OR where the last_name
is like $search
on all doctors.
try this:
$doctors = User::find(Auth::user()->id)
->doctors()
->orderBy($sortBy, $sort)
->where(function ($query) use ($search) {
return $query->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);
edited Nov 16 '18 at 16:13
answered Nov 16 '18 at 16:03
RemulRemul
1,3451414
1,3451414
add a comment |
add a comment |
You need to use when and group where
$search = $request->input('search');
$doctors = User::find(Auth::user()->id)
->doctors()
->when($search, function($query) use ($search){
$query->where(function ($query) use ($search) {
$query
->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 . '%');
})
})
->orderBy('...')
->paginate(10);
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
@IndianCoding To make sure I am fully understanding this code, thewhen
clause will essentially replace myif
check to see if a search query has been submitted?
– Xerakon
Nov 16 '18 at 17:12
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
add a comment |
You need to use when and group where
$search = $request->input('search');
$doctors = User::find(Auth::user()->id)
->doctors()
->when($search, function($query) use ($search){
$query->where(function ($query) use ($search) {
$query
->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 . '%');
})
})
->orderBy('...')
->paginate(10);
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
@IndianCoding To make sure I am fully understanding this code, thewhen
clause will essentially replace myif
check to see if a search query has been submitted?
– Xerakon
Nov 16 '18 at 17:12
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
add a comment |
You need to use when and group where
$search = $request->input('search');
$doctors = User::find(Auth::user()->id)
->doctors()
->when($search, function($query) use ($search){
$query->where(function ($query) use ($search) {
$query
->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 . '%');
})
})
->orderBy('...')
->paginate(10);
You need to use when and group where
$search = $request->input('search');
$doctors = User::find(Auth::user()->id)
->doctors()
->when($search, function($query) use ($search){
$query->where(function ($query) use ($search) {
$query
->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 . '%');
})
})
->orderBy('...')
->paginate(10);
edited Nov 16 '18 at 16:23
answered Nov 16 '18 at 16:16
IndianCodingIndianCoding
1,0441110
1,0441110
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
@IndianCoding To make sure I am fully understanding this code, thewhen
clause will essentially replace myif
check to see if a search query has been submitted?
– Xerakon
Nov 16 '18 at 17:12
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
add a comment |
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
@IndianCoding To make sure I am fully understanding this code, thewhen
clause will essentially replace myif
check to see if a search query has been submitted?
– Xerakon
Nov 16 '18 at 17:12
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
Yes, looked at my sources - where grouping required. Corrected.
– IndianCoding
Nov 16 '18 at 16:21
@IndianCoding To make sure I am fully understanding this code, the
when
clause will essentially replace my if
check to see if a search query has been submitted?– Xerakon
Nov 16 '18 at 17:12
@IndianCoding To make sure I am fully understanding this code, the
when
clause will essentially replace my if
check to see if a search query has been submitted?– Xerakon
Nov 16 '18 at 17:12
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
@Xerakon, You are right.
– IndianCoding
Nov 16 '18 at 17:35
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%2f53341289%2flaravel-limit-results-by-many-many-relationship%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