write where not in eloquent












0















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










share|improve this question























  • 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
















0















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










share|improve this question























  • 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














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












4 Answers
4






active

oldest

votes


















0














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





share|improve this answer





















  • 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














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.






share|improve this answer































    0














    To write NOT in Eloquent, use != instead of = in your WHERE clause.



    It will look like this:



    `LockDate::where('start_at' , '!=' , $end)->get();`





    share|improve this answer
























    • 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



















    0














    there is actually a whereDate() for eloquent date filtering.



    LockDate::whereDate('start_at' , '>=' , $end)
    ->whereDate('end_at' , '<=' , $start)
    ->get();





    share|improve this answer
























    • Right . Thanks . What about 'not' ?

      – Hamidreza
      Nov 21 '18 at 15:51











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









    0














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





    share|improve this answer





















    • 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


















    0














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





    share|improve this answer





















    • 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
















    0












    0








    0







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





    share|improve this answer















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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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















    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 16:17









        John HalseyJohn Halsey

        898825




        898825























            0














            To write NOT in Eloquent, use != instead of = in your WHERE clause.



            It will look like this:



            `LockDate::where('start_at' , '!=' , $end)->get();`





            share|improve this answer
























            • 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
















            0














            To write NOT in Eloquent, use != instead of = in your WHERE clause.



            It will look like this:



            `LockDate::where('start_at' , '!=' , $end)->get();`





            share|improve this answer
























            • 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














            0












            0








            0







            To write NOT in Eloquent, use != instead of = in your WHERE clause.



            It will look like this:



            `LockDate::where('start_at' , '!=' , $end)->get();`





            share|improve this answer













            To write NOT in Eloquent, use != instead of = in your WHERE clause.



            It will look like this:



            `LockDate::where('start_at' , '!=' , $end)->get();`






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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











            0














            there is actually a whereDate() for eloquent date filtering.



            LockDate::whereDate('start_at' , '>=' , $end)
            ->whereDate('end_at' , '<=' , $start)
            ->get();





            share|improve this answer
























            • Right . Thanks . What about 'not' ?

              – Hamidreza
              Nov 21 '18 at 15:51
















            0














            there is actually a whereDate() for eloquent date filtering.



            LockDate::whereDate('start_at' , '>=' , $end)
            ->whereDate('end_at' , '<=' , $start)
            ->get();





            share|improve this answer
























            • Right . Thanks . What about 'not' ?

              – Hamidreza
              Nov 21 '18 at 15:51














            0












            0








            0







            there is actually a whereDate() for eloquent date filtering.



            LockDate::whereDate('start_at' , '>=' , $end)
            ->whereDate('end_at' , '<=' , $start)
            ->get();





            share|improve this answer













            there is actually a whereDate() for eloquent date filtering.



            LockDate::whereDate('start_at' , '>=' , $end)
            ->whereDate('end_at' , '<=' , $start)
            ->get();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 13:44









            kapitankapitan

            668411




            668411













            • 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





            Right . Thanks . What about 'not' ?

            – Hamidreza
            Nov 21 '18 at 15:51


















            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.




            draft saved


            draft discarded














            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





















































            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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)