Laravel - Optional where and whereJsonConatins not working correctly












1














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.



enter image description here



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  









share|improve this question
























  • 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
















1














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.



enter image description here



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  









share|improve this question
























  • 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














1












1








1


0





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.



enter image description here



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  









share|improve this question















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.



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












2 Answers
2






active

oldest

votes


















0














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);





share|improve this answer





















  • 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










  • 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



















0














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






share|improve this answer























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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);





share|improve this answer





















  • 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










  • 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
















0














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);





share|improve this answer





















  • 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










  • 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














0












0








0






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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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










  • 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













0














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






share|improve this answer























  • 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
















0














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






share|improve this answer























  • 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














0












0








0






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas