write where not in eloquent
I want to write this SQL using eloquent in laravel 5.6 application
select * from `lock_dates` where not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
I don't know how to write not!
this is my eloquent code so far
LockDate::where('start_at' , '>=' , $end)
->orWhere('end_at' , '<=' , $start)
->get();
this is the SQL query that eloquent run behind the scenes which is the same SQL with I want except the not
0 => array:3 [▼
"query" => "select * from lock_dates where start_at >= ? or end_at <= ?"
"bindings" => array:2 [▼
0 => "2018-11-25 23:59:59"
1 => "2018-11-21 00:00:00"
]
"time" => 1.98
]
thanks in advance
php laravel eloquent
add a comment |
I want to write this SQL using eloquent in laravel 5.6 application
select * from `lock_dates` where not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
I don't know how to write not!
this is my eloquent code so far
LockDate::where('start_at' , '>=' , $end)
->orWhere('end_at' , '<=' , $start)
->get();
this is the SQL query that eloquent run behind the scenes which is the same SQL with I want except the not
0 => array:3 [▼
"query" => "select * from lock_dates where start_at >= ? or end_at <= ?"
"bindings" => array:2 [▼
0 => "2018-11-25 23:59:59"
1 => "2018-11-21 00:00:00"
]
"time" => 1.98
]
thanks in advance
php laravel eloquent
question, shouldn't it be start_at >= 2018-11-21 and end_at<= 2018-11-25 ?
– kapitan
Nov 21 '18 at 13:51
Eloquent essentially uses the builder, you can look at the source to determine exactly what you need: github.com/laravel/framework/blob/5.6/src/Illuminate/Database/…
– adam
Nov 21 '18 at 16:28
@kapitan no. I want all records Except [records that end before 21 or records that start after 25 ]
– Hamidreza
Nov 21 '18 at 16:47
add a comment |
I want to write this SQL using eloquent in laravel 5.6 application
select * from `lock_dates` where not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
I don't know how to write not!
this is my eloquent code so far
LockDate::where('start_at' , '>=' , $end)
->orWhere('end_at' , '<=' , $start)
->get();
this is the SQL query that eloquent run behind the scenes which is the same SQL with I want except the not
0 => array:3 [▼
"query" => "select * from lock_dates where start_at >= ? or end_at <= ?"
"bindings" => array:2 [▼
0 => "2018-11-25 23:59:59"
1 => "2018-11-21 00:00:00"
]
"time" => 1.98
]
thanks in advance
php laravel eloquent
I want to write this SQL using eloquent in laravel 5.6 application
select * from `lock_dates` where not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
I don't know how to write not!
this is my eloquent code so far
LockDate::where('start_at' , '>=' , $end)
->orWhere('end_at' , '<=' , $start)
->get();
this is the SQL query that eloquent run behind the scenes which is the same SQL with I want except the not
0 => array:3 [▼
"query" => "select * from lock_dates where start_at >= ? or end_at <= ?"
"bindings" => array:2 [▼
0 => "2018-11-25 23:59:59"
1 => "2018-11-21 00:00:00"
]
"time" => 1.98
]
thanks in advance
php laravel eloquent
php laravel eloquent
asked Nov 21 '18 at 13:33
HamidrezaHamidreza
8412
8412
question, shouldn't it be start_at >= 2018-11-21 and end_at<= 2018-11-25 ?
– kapitan
Nov 21 '18 at 13:51
Eloquent essentially uses the builder, you can look at the source to determine exactly what you need: github.com/laravel/framework/blob/5.6/src/Illuminate/Database/…
– adam
Nov 21 '18 at 16:28
@kapitan no. I want all records Except [records that end before 21 or records that start after 25 ]
– Hamidreza
Nov 21 '18 at 16:47
add a comment |
question, shouldn't it be start_at >= 2018-11-21 and end_at<= 2018-11-25 ?
– kapitan
Nov 21 '18 at 13:51
Eloquent essentially uses the builder, you can look at the source to determine exactly what you need: github.com/laravel/framework/blob/5.6/src/Illuminate/Database/…
– adam
Nov 21 '18 at 16:28
@kapitan no. I want all records Except [records that end before 21 or records that start after 25 ]
– Hamidreza
Nov 21 '18 at 16:47
question, shouldn't it be start_at >= 2018-11-21 and end_at<= 2018-11-25 ?
– kapitan
Nov 21 '18 at 13:51
question, shouldn't it be start_at >= 2018-11-21 and end_at<= 2018-11-25 ?
– kapitan
Nov 21 '18 at 13:51
Eloquent essentially uses the builder, you can look at the source to determine exactly what you need: github.com/laravel/framework/blob/5.6/src/Illuminate/Database/…
– adam
Nov 21 '18 at 16:28
Eloquent essentially uses the builder, you can look at the source to determine exactly what you need: github.com/laravel/framework/blob/5.6/src/Illuminate/Database/…
– adam
Nov 21 '18 at 16:28
@kapitan no. I want all records Except [records that end before 21 or records that start after 25 ]
– Hamidreza
Nov 21 '18 at 16:47
@kapitan no. I want all records Except [records that end before 21 or records that start after 25 ]
– Hamidreza
Nov 21 '18 at 16:47
add a comment |
4 Answers
4
active
oldest
votes
Actually you don't need "not", your condition:
not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
is equal to:
`start_at` <= '2018-11-25 23:59:59' AND `end_at` >= '2018-11-21 00:00:00'
So:
$lockDates = LockDate::where('start_at' , '<=' , $end)
->where('end_at' , '=>' , $start)
->get();
1
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
add a comment |
I think kapitan is closest, but should be like this?
LockDate::whereDate('start_at' , '<' , $end)
->whereDate('end_at' , '>' , $start)
->get();
This is like checking your not
request because the dates are reversed.
add a comment |
To write NOT
in Eloquent, use !=
instead of =
in your WHERE
clause.
It will look like this:
`LockDate::where('start_at' , '!=' , $end)->get();`
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
add a comment |
there is actually a whereDate() for eloquent date filtering.
LockDate::whereDate('start_at' , '>=' , $end)
->whereDate('end_at' , '<=' , $start)
->get();
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
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%2f53413220%2fwrite-where-not-in-eloquent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Actually you don't need "not", your condition:
not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
is equal to:
`start_at` <= '2018-11-25 23:59:59' AND `end_at` >= '2018-11-21 00:00:00'
So:
$lockDates = LockDate::where('start_at' , '<=' , $end)
->where('end_at' , '=>' , $start)
->get();
1
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
add a comment |
Actually you don't need "not", your condition:
not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
is equal to:
`start_at` <= '2018-11-25 23:59:59' AND `end_at` >= '2018-11-21 00:00:00'
So:
$lockDates = LockDate::where('start_at' , '<=' , $end)
->where('end_at' , '=>' , $start)
->get();
1
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
add a comment |
Actually you don't need "not", your condition:
not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
is equal to:
`start_at` <= '2018-11-25 23:59:59' AND `end_at` >= '2018-11-21 00:00:00'
So:
$lockDates = LockDate::where('start_at' , '<=' , $end)
->where('end_at' , '=>' , $start)
->get();
Actually you don't need "not", your condition:
not (`start_at` >= '2018-11-25 23:59:59' or `end_at` <= '2018-11-21 00:00:00')
is equal to:
`start_at` <= '2018-11-25 23:59:59' AND `end_at` >= '2018-11-21 00:00:00'
So:
$lockDates = LockDate::where('start_at' , '<=' , $end)
->where('end_at' , '=>' , $start)
->get();
edited Nov 21 '18 at 13:52
answered Nov 21 '18 at 13:50
IndianCodingIndianCoding
1,0441110
1,0441110
1
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
add a comment |
1
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
1
1
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
I think the equals signs need to be removed, since the original query had them.
– aynber
Nov 21 '18 at 13:51
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
Corrected answer.
– IndianCoding
Nov 21 '18 at 13:53
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
you are right. there is no need to not my condition in this case . but out of curiosity if I needed to not the conditions . how should I do that? it may help the others with the same problem too. thanks
– Hamidreza
Nov 21 '18 at 17:29
add a comment |
I think kapitan is closest, but should be like this?
LockDate::whereDate('start_at' , '<' , $end)
->whereDate('end_at' , '>' , $start)
->get();
This is like checking your not
request because the dates are reversed.
add a comment |
I think kapitan is closest, but should be like this?
LockDate::whereDate('start_at' , '<' , $end)
->whereDate('end_at' , '>' , $start)
->get();
This is like checking your not
request because the dates are reversed.
add a comment |
I think kapitan is closest, but should be like this?
LockDate::whereDate('start_at' , '<' , $end)
->whereDate('end_at' , '>' , $start)
->get();
This is like checking your not
request because the dates are reversed.
I think kapitan is closest, but should be like this?
LockDate::whereDate('start_at' , '<' , $end)
->whereDate('end_at' , '>' , $start)
->get();
This is like checking your not
request because the dates are reversed.
answered Nov 21 '18 at 16:17
John HalseyJohn Halsey
898825
898825
add a comment |
add a comment |
To write NOT
in Eloquent, use !=
instead of =
in your WHERE
clause.
It will look like this:
`LockDate::where('start_at' , '!=' , $end)->get();`
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
add a comment |
To write NOT
in Eloquent, use !=
instead of =
in your WHERE
clause.
It will look like this:
`LockDate::where('start_at' , '!=' , $end)->get();`
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
add a comment |
To write NOT
in Eloquent, use !=
instead of =
in your WHERE
clause.
It will look like this:
`LockDate::where('start_at' , '!=' , $end)->get();`
To write NOT
in Eloquent, use !=
instead of =
in your WHERE
clause.
It will look like this:
`LockDate::where('start_at' , '!=' , $end)->get();`
answered Nov 21 '18 at 13:38
Nick DawesNick Dawes
919
919
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
add a comment |
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
This code dose not generate the sql i wrote . The not implies on both condition
– Hamidreza
Nov 21 '18 at 13:43
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
LockDate::whereDate('start_at' , '!=' , $end) ->whereDate('end_at' , '!=' , $start) ->get();
– Nick Dawes
Nov 21 '18 at 14:12
add a comment |
there is actually a whereDate() for eloquent date filtering.
LockDate::whereDate('start_at' , '>=' , $end)
->whereDate('end_at' , '<=' , $start)
->get();
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
add a comment |
there is actually a whereDate() for eloquent date filtering.
LockDate::whereDate('start_at' , '>=' , $end)
->whereDate('end_at' , '<=' , $start)
->get();
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
add a comment |
there is actually a whereDate() for eloquent date filtering.
LockDate::whereDate('start_at' , '>=' , $end)
->whereDate('end_at' , '<=' , $start)
->get();
there is actually a whereDate() for eloquent date filtering.
LockDate::whereDate('start_at' , '>=' , $end)
->whereDate('end_at' , '<=' , $start)
->get();
answered Nov 21 '18 at 13:44
kapitankapitan
668411
668411
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
add a comment |
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
Right . Thanks . What about 'not' ?
– Hamidreza
Nov 21 '18 at 15:51
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%2f53413220%2fwrite-where-not-in-eloquent%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
question, shouldn't it be start_at >= 2018-11-21 and end_at<= 2018-11-25 ?
– kapitan
Nov 21 '18 at 13:51
Eloquent essentially uses the builder, you can look at the source to determine exactly what you need: github.com/laravel/framework/blob/5.6/src/Illuminate/Database/…
– adam
Nov 21 '18 at 16:28
@kapitan no. I want all records Except [records that end before 21 or records that start after 25 ]
– Hamidreza
Nov 21 '18 at 16:47