Laravel - Optional where and whereJsonConatins not working correctly
PHP 7.2.10
Laravel 5.6.39
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
$videos = Video::query();
if ( isset( $queryParams['category'] ) ){
$videos = $videos->where('video_category', '=', $queryParams['category']);
}
if ( isset( $queryParams['language'] ) ){
$videos = $videos->where('video_language', '=', $queryParams['language']);
}
$videos = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
});
$videos = $videos->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
});
$videos = $videos->orderBy('video_priority', 'desc')->latest()->paginate(10);
return $videos;
language and category are optional filters in current condition let's consider language filter, if there is no video with the selected language, even then I will get videos with status 10 and 11,
if I will use whereJsonContains
above the filters then also it is giving wrong result, how I should use whereJsonContains, whereJsonDoesNotContains and optional wheres correctly?
Note: may be helpful to get the issue correctly
You can also reverse the situation
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
below image showing manual tagger hindi videos, but it can be seen the choosen filters are auto tagger and Indonesian.
Query generated for
http://mysite.dev/admin/tagging-in-progress?_token=d4jQOLDixYSEXhMR2z8OihVPQ2ZKTezJhk3tIWJg&category=manual_tagger&language=maltese&video_status=&submit=
is
select * from `videos` where `video_category` = 'manual_tagger' and `video_language` = 'english' and json_contains(`video_status_ids`, 9) or json_contains(`video_status_ids`, 29) order by `video_priority` desc, `created_at` desc limit 10 offset 0
php laravel eloquent
add a comment |
PHP 7.2.10
Laravel 5.6.39
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
$videos = Video::query();
if ( isset( $queryParams['category'] ) ){
$videos = $videos->where('video_category', '=', $queryParams['category']);
}
if ( isset( $queryParams['language'] ) ){
$videos = $videos->where('video_language', '=', $queryParams['language']);
}
$videos = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
});
$videos = $videos->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
});
$videos = $videos->orderBy('video_priority', 'desc')->latest()->paginate(10);
return $videos;
language and category are optional filters in current condition let's consider language filter, if there is no video with the selected language, even then I will get videos with status 10 and 11,
if I will use whereJsonContains
above the filters then also it is giving wrong result, how I should use whereJsonContains, whereJsonDoesNotContains and optional wheres correctly?
Note: may be helpful to get the issue correctly
You can also reverse the situation
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
below image showing manual tagger hindi videos, but it can be seen the choosen filters are auto tagger and Indonesian.
Query generated for
http://mysite.dev/admin/tagging-in-progress?_token=d4jQOLDixYSEXhMR2z8OihVPQ2ZKTezJhk3tIWJg&category=manual_tagger&language=maltese&video_status=&submit=
is
select * from `videos` where `video_category` = 'manual_tagger' and `video_language` = 'english' and json_contains(`video_status_ids`, 9) or json_contains(`video_status_ids`, 29) order by `video_priority` desc, `created_at` desc limit 10 offset 0
php laravel eloquent
Comments are not for extended discussion; this conversation has been moved to chat.
– Yvette Colomb♦
Dec 29 '18 at 9:37
@JonasStaudenmeir please have a look at updates on question and also the chat, may be only you can help me.
– Prafulla Kumar Sahu
Jan 2 at 10:03
add a comment |
PHP 7.2.10
Laravel 5.6.39
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
$videos = Video::query();
if ( isset( $queryParams['category'] ) ){
$videos = $videos->where('video_category', '=', $queryParams['category']);
}
if ( isset( $queryParams['language'] ) ){
$videos = $videos->where('video_language', '=', $queryParams['language']);
}
$videos = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
});
$videos = $videos->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
});
$videos = $videos->orderBy('video_priority', 'desc')->latest()->paginate(10);
return $videos;
language and category are optional filters in current condition let's consider language filter, if there is no video with the selected language, even then I will get videos with status 10 and 11,
if I will use whereJsonContains
above the filters then also it is giving wrong result, how I should use whereJsonContains, whereJsonDoesNotContains and optional wheres correctly?
Note: may be helpful to get the issue correctly
You can also reverse the situation
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
below image showing manual tagger hindi videos, but it can be seen the choosen filters are auto tagger and Indonesian.
Query generated for
http://mysite.dev/admin/tagging-in-progress?_token=d4jQOLDixYSEXhMR2z8OihVPQ2ZKTezJhk3tIWJg&category=manual_tagger&language=maltese&video_status=&submit=
is
select * from `videos` where `video_category` = 'manual_tagger' and `video_language` = 'english' and json_contains(`video_status_ids`, 9) or json_contains(`video_status_ids`, 29) order by `video_priority` desc, `created_at` desc limit 10 offset 0
php laravel eloquent
PHP 7.2.10
Laravel 5.6.39
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
$videos = Video::query();
if ( isset( $queryParams['category'] ) ){
$videos = $videos->where('video_category', '=', $queryParams['category']);
}
if ( isset( $queryParams['language'] ) ){
$videos = $videos->where('video_language', '=', $queryParams['language']);
}
$videos = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
});
$videos = $videos->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
});
$videos = $videos->orderBy('video_priority', 'desc')->latest()->paginate(10);
return $videos;
language and category are optional filters in current condition let's consider language filter, if there is no video with the selected language, even then I will get videos with status 10 and 11,
if I will use whereJsonContains
above the filters then also it is giving wrong result, how I should use whereJsonContains, whereJsonDoesNotContains and optional wheres correctly?
Note: may be helpful to get the issue correctly
You can also reverse the situation
$statuses = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
$notStatuses = [14, 15, 16, 17, 18, 19];
below image showing manual tagger hindi videos, but it can be seen the choosen filters are auto tagger and Indonesian.
Query generated for
http://mysite.dev/admin/tagging-in-progress?_token=d4jQOLDixYSEXhMR2z8OihVPQ2ZKTezJhk3tIWJg&category=manual_tagger&language=maltese&video_status=&submit=
is
select * from `videos` where `video_category` = 'manual_tagger' and `video_language` = 'english' and json_contains(`video_status_ids`, 9) or json_contains(`video_status_ids`, 29) order by `video_priority` desc, `created_at` desc limit 10 offset 0
php laravel eloquent
php laravel eloquent
edited Jan 2 at 10:02
Prafulla Kumar Sahu
asked Dec 28 '18 at 9:31
Prafulla Kumar SahuPrafulla Kumar Sahu
2,93662146
2,93662146
Comments are not for extended discussion; this conversation has been moved to chat.
– Yvette Colomb♦
Dec 29 '18 at 9:37
@JonasStaudenmeir please have a look at updates on question and also the chat, may be only you can help me.
– Prafulla Kumar Sahu
Jan 2 at 10:03
add a comment |
Comments are not for extended discussion; this conversation has been moved to chat.
– Yvette Colomb♦
Dec 29 '18 at 9:37
@JonasStaudenmeir please have a look at updates on question and also the chat, may be only you can help me.
– Prafulla Kumar Sahu
Jan 2 at 10:03
Comments are not for extended discussion; this conversation has been moved to chat.
– Yvette Colomb♦
Dec 29 '18 at 9:37
Comments are not for extended discussion; this conversation has been moved to chat.
– Yvette Colomb♦
Dec 29 '18 at 9:37
@JonasStaudenmeir please have a look at updates on question and also the chat, may be only you can help me.
– Prafulla Kumar Sahu
Jan 2 at 10:03
@JonasStaudenmeir please have a look at updates on question and also the chat, may be only you can help me.
– Prafulla Kumar Sahu
Jan 2 at 10:03
add a comment |
2 Answers
2
active
oldest
votes
just try this one
$video = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
})->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
})
->orderBy('video_priority', 'desc')->latest()->paginate(10);
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the allwhere
conditions in single query ..
– ARUN Madathil
Dec 28 '18 at 10:17
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
add a comment |
try after change :
WhereJsonContains('video_status_ids', $status)
to
whereRaw('JSON_CONTAINS(video_status_ids, "$status")')
But, Best way is normalize DB structure.
Example:
post like table :
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1,2 |
| 2 | 1,5 |
+---------+---------+
normalize tabel
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 5 |
+---------+---------+
I don't know is it usable in your condition or not. but if is it then normalize table
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
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%2f53956317%2flaravel-optional-where-and-wherejsonconatins-not-working-correctly%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
just try this one
$video = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
})->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
})
->orderBy('video_priority', 'desc')->latest()->paginate(10);
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the allwhere
conditions in single query ..
– ARUN Madathil
Dec 28 '18 at 10:17
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
add a comment |
just try this one
$video = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
})->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
})
->orderBy('video_priority', 'desc')->latest()->paginate(10);
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the allwhere
conditions in single query ..
– ARUN Madathil
Dec 28 '18 at 10:17
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
add a comment |
just try this one
$video = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
})->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
})
->orderBy('video_priority', 'desc')->latest()->paginate(10);
just try this one
$video = $videos->where(function($query) use($statuses) {
foreach($statuses as $status) {
$query->orWhereJsonContains('video_status_ids', $status);
}
})->where(function($query) use($notStatuses) {
foreach($notStatuses as $status) {
$query->whereJsonDoesntContain('video_status_ids', $status);
}
})
->orderBy('video_priority', 'desc')->latest()->paginate(10);
answered Dec 28 '18 at 10:13
ARUN MadathilARUN Madathil
1097
1097
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the allwhere
conditions in single query ..
– ARUN Madathil
Dec 28 '18 at 10:17
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
add a comment |
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the allwhere
conditions in single query ..
– ARUN Madathil
Dec 28 '18 at 10:17
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
Please see the question, it also has optional filters.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:13
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the all
where
conditions in single query ..– ARUN Madathil
Dec 28 '18 at 10:17
@Prafulla Kumar Sahu , yes i saw the optional filter i just written the all
where
conditions in single query ..– ARUN Madathil
Dec 28 '18 at 10:17
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
Dear friend, I know that, but the question is different, the question is how to use optional where with whereJsonConatins and whereJsonDoesNotContains clause.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:18
add a comment |
try after change :
WhereJsonContains('video_status_ids', $status)
to
whereRaw('JSON_CONTAINS(video_status_ids, "$status")')
But, Best way is normalize DB structure.
Example:
post like table :
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1,2 |
| 2 | 1,5 |
+---------+---------+
normalize tabel
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 5 |
+---------+---------+
I don't know is it usable in your condition or not. but if is it then normalize table
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
add a comment |
try after change :
WhereJsonContains('video_status_ids', $status)
to
whereRaw('JSON_CONTAINS(video_status_ids, "$status")')
But, Best way is normalize DB structure.
Example:
post like table :
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1,2 |
| 2 | 1,5 |
+---------+---------+
normalize tabel
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 5 |
+---------+---------+
I don't know is it usable in your condition or not. but if is it then normalize table
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
add a comment |
try after change :
WhereJsonContains('video_status_ids', $status)
to
whereRaw('JSON_CONTAINS(video_status_ids, "$status")')
But, Best way is normalize DB structure.
Example:
post like table :
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1,2 |
| 2 | 1,5 |
+---------+---------+
normalize tabel
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 5 |
+---------+---------+
I don't know is it usable in your condition or not. but if is it then normalize table
try after change :
WhereJsonContains('video_status_ids', $status)
to
whereRaw('JSON_CONTAINS(video_status_ids, "$status")')
But, Best way is normalize DB structure.
Example:
post like table :
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1,2 |
| 2 | 1,5 |
+---------+---------+
normalize tabel
+---------+---------+
| user_id | post_id |
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 5 |
+---------+---------+
I don't know is it usable in your condition or not. but if is it then normalize table
edited Dec 28 '18 at 10:24
answered Dec 28 '18 at 10:08
Dhruv RavalDhruv Raval
1,043312
1,043312
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
add a comment |
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
I tried this but sadly, it is not giving a single record by the about normalization, there are some situation we are given and can not create or change it, just bound to work on it, you can understand it, right.
– Prafulla Kumar Sahu
Dec 28 '18 at 10:45
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%2f53956317%2flaravel-optional-where-and-wherejsonconatins-not-working-correctly%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
Comments are not for extended discussion; this conversation has been moved to chat.
– Yvette Colomb♦
Dec 29 '18 at 9:37
@JonasStaudenmeir please have a look at updates on question and also the chat, may be only you can help me.
– Prafulla Kumar Sahu
Jan 2 at 10:03