Laravel where('user_id') seems completely ignored
I have a problem where user_id
seems completely ignored by Laravels Eloquent ORM.
Pigeons Tableid user_id name father_id mother_id ringnumber
gender color created_at updated_at landcode
(these are my columns (if someone knows how to format this better, let me know))
I have a search from which routes a search parameter q
to my SearchController.php in which this function lives:
namespace AppHttpControllers;
use AppPigeon;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesInput;
class SearchController extends Controller
{
public function index()
{
$q = Input::get('query');
$userId = Auth::user()->id;
$pigeons = Pigeon::where([
['user_id', '=', $userId],
['name','LIKE','%'.$q.'%']
])
->orWhere('ringnumber','LIKE','%'.$q.'%')
->sortable()
->paginate(15);
dd($pigeons);
return view('backend.pigeon.pigeonlist')->with('pigeons', $pigeons);
}
}
For some reason this Eloquent query builder completely seems to ignore 'user_id', '=', $userId
and this is an important part because I only want to search for pigeons for the current logged in user.
Below is a result of such a query, the problem is that there are pigeons with all kinds of user_id
and not only the one user that searched for them.
LengthAwarePaginator {#259 ▼
#total: 150
#lastPage: 10
#items: Collection {#267 ▼
#items: array:15 [▼
0 => Pigeon {#268 ▶}
1 => Pigeon {#269 ▶}
2 => Pigeon {#270 ▶}
3 => Pigeon {#271 ▶}
4 => Pigeon {#272 ▶}
5 => Pigeon {#273 ▶}
6 => Pigeon {#274 ▶}
7 => Pigeon {#275 ▶}
8 => Pigeon {#276 ▶}
9 => Pigeon {#277 ▶}
10 => Pigeon {#278 ▶}
11 => Pigeon {#279 ▶}
12 => Pigeon {#280 ▶}
13 => Pigeon {#281 ▶}
14 => Pigeon {#282 ▶}
]
}
#perPage: 15
#currentPage: 1
#path: "http://mywebsite.test/pigeon/search"
#query:
#fragment: null
#pageName: "page"
+onEachSide: 3
}
Small note, I got some of my information from here: How to create multiple where clause query using Laravel Eloquent?
Problem solved:
First I had an orWhere which overruled the where so that was pretty stupid of me.
Second my real problem was that I was trying to get only records for the current logged in user that works via this code:
$pigeons = Pigeon::where('user_id', Auth::id())
->where(function($query) use ($q) {
$query->where('name', 'LIKE', '%'. $q .'%');
})
->sortable()
->paginate(15);
laravel
|
show 2 more comments
I have a problem where user_id
seems completely ignored by Laravels Eloquent ORM.
Pigeons Tableid user_id name father_id mother_id ringnumber
gender color created_at updated_at landcode
(these are my columns (if someone knows how to format this better, let me know))
I have a search from which routes a search parameter q
to my SearchController.php in which this function lives:
namespace AppHttpControllers;
use AppPigeon;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesInput;
class SearchController extends Controller
{
public function index()
{
$q = Input::get('query');
$userId = Auth::user()->id;
$pigeons = Pigeon::where([
['user_id', '=', $userId],
['name','LIKE','%'.$q.'%']
])
->orWhere('ringnumber','LIKE','%'.$q.'%')
->sortable()
->paginate(15);
dd($pigeons);
return view('backend.pigeon.pigeonlist')->with('pigeons', $pigeons);
}
}
For some reason this Eloquent query builder completely seems to ignore 'user_id', '=', $userId
and this is an important part because I only want to search for pigeons for the current logged in user.
Below is a result of such a query, the problem is that there are pigeons with all kinds of user_id
and not only the one user that searched for them.
LengthAwarePaginator {#259 ▼
#total: 150
#lastPage: 10
#items: Collection {#267 ▼
#items: array:15 [▼
0 => Pigeon {#268 ▶}
1 => Pigeon {#269 ▶}
2 => Pigeon {#270 ▶}
3 => Pigeon {#271 ▶}
4 => Pigeon {#272 ▶}
5 => Pigeon {#273 ▶}
6 => Pigeon {#274 ▶}
7 => Pigeon {#275 ▶}
8 => Pigeon {#276 ▶}
9 => Pigeon {#277 ▶}
10 => Pigeon {#278 ▶}
11 => Pigeon {#279 ▶}
12 => Pigeon {#280 ▶}
13 => Pigeon {#281 ▶}
14 => Pigeon {#282 ▶}
]
}
#perPage: 15
#currentPage: 1
#path: "http://mywebsite.test/pigeon/search"
#query:
#fragment: null
#pageName: "page"
+onEachSide: 3
}
Small note, I got some of my information from here: How to create multiple where clause query using Laravel Eloquent?
Problem solved:
First I had an orWhere which overruled the where so that was pretty stupid of me.
Second my real problem was that I was trying to get only records for the current logged in user that works via this code:
$pigeons = Pigeon::where('user_id', Auth::id())
->where(function($query) use ($q) {
$query->where('name', 'LIKE', '%'. $q .'%');
})
->sortable()
->paginate(15);
laravel
What's inringnumber
? Is that specific to a user too?
– Stuart
Nov 12 '18 at 17:09
No sir, ringnumber is not specific to user, it's specific to a pigeon
– ArneDB
Nov 12 '18 at 17:11
Possible duplicate of Search function return unwanted results in Laravel
– Devon
Nov 12 '18 at 17:23
1
The problem happens when you use->orWhere
you say either the one above is true, or this is true.
– nakov
Nov 12 '18 at 17:24
Yes that's correct, the orWhere was overruling the where, which was causing it to neglate the user_id
– ArneDB
Nov 12 '18 at 20:07
|
show 2 more comments
I have a problem where user_id
seems completely ignored by Laravels Eloquent ORM.
Pigeons Tableid user_id name father_id mother_id ringnumber
gender color created_at updated_at landcode
(these are my columns (if someone knows how to format this better, let me know))
I have a search from which routes a search parameter q
to my SearchController.php in which this function lives:
namespace AppHttpControllers;
use AppPigeon;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesInput;
class SearchController extends Controller
{
public function index()
{
$q = Input::get('query');
$userId = Auth::user()->id;
$pigeons = Pigeon::where([
['user_id', '=', $userId],
['name','LIKE','%'.$q.'%']
])
->orWhere('ringnumber','LIKE','%'.$q.'%')
->sortable()
->paginate(15);
dd($pigeons);
return view('backend.pigeon.pigeonlist')->with('pigeons', $pigeons);
}
}
For some reason this Eloquent query builder completely seems to ignore 'user_id', '=', $userId
and this is an important part because I only want to search for pigeons for the current logged in user.
Below is a result of such a query, the problem is that there are pigeons with all kinds of user_id
and not only the one user that searched for them.
LengthAwarePaginator {#259 ▼
#total: 150
#lastPage: 10
#items: Collection {#267 ▼
#items: array:15 [▼
0 => Pigeon {#268 ▶}
1 => Pigeon {#269 ▶}
2 => Pigeon {#270 ▶}
3 => Pigeon {#271 ▶}
4 => Pigeon {#272 ▶}
5 => Pigeon {#273 ▶}
6 => Pigeon {#274 ▶}
7 => Pigeon {#275 ▶}
8 => Pigeon {#276 ▶}
9 => Pigeon {#277 ▶}
10 => Pigeon {#278 ▶}
11 => Pigeon {#279 ▶}
12 => Pigeon {#280 ▶}
13 => Pigeon {#281 ▶}
14 => Pigeon {#282 ▶}
]
}
#perPage: 15
#currentPage: 1
#path: "http://mywebsite.test/pigeon/search"
#query:
#fragment: null
#pageName: "page"
+onEachSide: 3
}
Small note, I got some of my information from here: How to create multiple where clause query using Laravel Eloquent?
Problem solved:
First I had an orWhere which overruled the where so that was pretty stupid of me.
Second my real problem was that I was trying to get only records for the current logged in user that works via this code:
$pigeons = Pigeon::where('user_id', Auth::id())
->where(function($query) use ($q) {
$query->where('name', 'LIKE', '%'. $q .'%');
})
->sortable()
->paginate(15);
laravel
I have a problem where user_id
seems completely ignored by Laravels Eloquent ORM.
Pigeons Tableid user_id name father_id mother_id ringnumber
gender color created_at updated_at landcode
(these are my columns (if someone knows how to format this better, let me know))
I have a search from which routes a search parameter q
to my SearchController.php in which this function lives:
namespace AppHttpControllers;
use AppPigeon;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesInput;
class SearchController extends Controller
{
public function index()
{
$q = Input::get('query');
$userId = Auth::user()->id;
$pigeons = Pigeon::where([
['user_id', '=', $userId],
['name','LIKE','%'.$q.'%']
])
->orWhere('ringnumber','LIKE','%'.$q.'%')
->sortable()
->paginate(15);
dd($pigeons);
return view('backend.pigeon.pigeonlist')->with('pigeons', $pigeons);
}
}
For some reason this Eloquent query builder completely seems to ignore 'user_id', '=', $userId
and this is an important part because I only want to search for pigeons for the current logged in user.
Below is a result of such a query, the problem is that there are pigeons with all kinds of user_id
and not only the one user that searched for them.
LengthAwarePaginator {#259 ▼
#total: 150
#lastPage: 10
#items: Collection {#267 ▼
#items: array:15 [▼
0 => Pigeon {#268 ▶}
1 => Pigeon {#269 ▶}
2 => Pigeon {#270 ▶}
3 => Pigeon {#271 ▶}
4 => Pigeon {#272 ▶}
5 => Pigeon {#273 ▶}
6 => Pigeon {#274 ▶}
7 => Pigeon {#275 ▶}
8 => Pigeon {#276 ▶}
9 => Pigeon {#277 ▶}
10 => Pigeon {#278 ▶}
11 => Pigeon {#279 ▶}
12 => Pigeon {#280 ▶}
13 => Pigeon {#281 ▶}
14 => Pigeon {#282 ▶}
]
}
#perPage: 15
#currentPage: 1
#path: "http://mywebsite.test/pigeon/search"
#query:
#fragment: null
#pageName: "page"
+onEachSide: 3
}
Small note, I got some of my information from here: How to create multiple where clause query using Laravel Eloquent?
Problem solved:
First I had an orWhere which overruled the where so that was pretty stupid of me.
Second my real problem was that I was trying to get only records for the current logged in user that works via this code:
$pigeons = Pigeon::where('user_id', Auth::id())
->where(function($query) use ($q) {
$query->where('name', 'LIKE', '%'. $q .'%');
})
->sortable()
->paginate(15);
laravel
laravel
edited Nov 13 '18 at 17:33
asked Nov 12 '18 at 17:02
ArneDB
13
13
What's inringnumber
? Is that specific to a user too?
– Stuart
Nov 12 '18 at 17:09
No sir, ringnumber is not specific to user, it's specific to a pigeon
– ArneDB
Nov 12 '18 at 17:11
Possible duplicate of Search function return unwanted results in Laravel
– Devon
Nov 12 '18 at 17:23
1
The problem happens when you use->orWhere
you say either the one above is true, or this is true.
– nakov
Nov 12 '18 at 17:24
Yes that's correct, the orWhere was overruling the where, which was causing it to neglate the user_id
– ArneDB
Nov 12 '18 at 20:07
|
show 2 more comments
What's inringnumber
? Is that specific to a user too?
– Stuart
Nov 12 '18 at 17:09
No sir, ringnumber is not specific to user, it's specific to a pigeon
– ArneDB
Nov 12 '18 at 17:11
Possible duplicate of Search function return unwanted results in Laravel
– Devon
Nov 12 '18 at 17:23
1
The problem happens when you use->orWhere
you say either the one above is true, or this is true.
– nakov
Nov 12 '18 at 17:24
Yes that's correct, the orWhere was overruling the where, which was causing it to neglate the user_id
– ArneDB
Nov 12 '18 at 20:07
What's in
ringnumber
? Is that specific to a user too?– Stuart
Nov 12 '18 at 17:09
What's in
ringnumber
? Is that specific to a user too?– Stuart
Nov 12 '18 at 17:09
No sir, ringnumber is not specific to user, it's specific to a pigeon
– ArneDB
Nov 12 '18 at 17:11
No sir, ringnumber is not specific to user, it's specific to a pigeon
– ArneDB
Nov 12 '18 at 17:11
Possible duplicate of Search function return unwanted results in Laravel
– Devon
Nov 12 '18 at 17:23
Possible duplicate of Search function return unwanted results in Laravel
– Devon
Nov 12 '18 at 17:23
1
1
The problem happens when you use
->orWhere
you say either the one above is true, or this is true.– nakov
Nov 12 '18 at 17:24
The problem happens when you use
->orWhere
you say either the one above is true, or this is true.– nakov
Nov 12 '18 at 17:24
Yes that's correct, the orWhere was overruling the where, which was causing it to neglate the user_id
– ArneDB
Nov 12 '18 at 20:07
Yes that's correct, the orWhere was overruling the where, which was causing it to neglate the user_id
– ArneDB
Nov 12 '18 at 20:07
|
show 2 more comments
1 Answer
1
active
oldest
votes
That's the right behavior since you are adding an orWhere
clause after the where.
This will result in a query like this:
SELECT * FROM your_table WHERE (user_id = xxx) OR (some condition that results true)
Since false OR true
is equal to true
, the first clause is being ignored (because the second is true)
1
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
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%2f53266870%2flaravel-whereuser-id-seems-completely-ignored%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's the right behavior since you are adding an orWhere
clause after the where.
This will result in a query like this:
SELECT * FROM your_table WHERE (user_id = xxx) OR (some condition that results true)
Since false OR true
is equal to true
, the first clause is being ignored (because the second is true)
1
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
add a comment |
That's the right behavior since you are adding an orWhere
clause after the where.
This will result in a query like this:
SELECT * FROM your_table WHERE (user_id = xxx) OR (some condition that results true)
Since false OR true
is equal to true
, the first clause is being ignored (because the second is true)
1
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
add a comment |
That's the right behavior since you are adding an orWhere
clause after the where.
This will result in a query like this:
SELECT * FROM your_table WHERE (user_id = xxx) OR (some condition that results true)
Since false OR true
is equal to true
, the first clause is being ignored (because the second is true)
That's the right behavior since you are adding an orWhere
clause after the where.
This will result in a query like this:
SELECT * FROM your_table WHERE (user_id = xxx) OR (some condition that results true)
Since false OR true
is equal to true
, the first clause is being ignored (because the second is true)
answered Nov 13 '18 at 17:37
Elias Soares
2,80811237
2,80811237
1
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
add a comment |
1
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
1
1
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
Indeed, answer was already given, but since this is the only one I can accept, I'll accep this correct answer! Thanks for taking the time to write it out like this!
– ArneDB
Nov 14 '18 at 9:02
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53266870%2flaravel-whereuser-id-seems-completely-ignored%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
What's in
ringnumber
? Is that specific to a user too?– Stuart
Nov 12 '18 at 17:09
No sir, ringnumber is not specific to user, it's specific to a pigeon
– ArneDB
Nov 12 '18 at 17:11
Possible duplicate of Search function return unwanted results in Laravel
– Devon
Nov 12 '18 at 17:23
1
The problem happens when you use
->orWhere
you say either the one above is true, or this is true.– nakov
Nov 12 '18 at 17:24
Yes that's correct, the orWhere was overruling the where, which was causing it to neglate the user_id
– ArneDB
Nov 12 '18 at 20:07