Delete Elequent records older than 60 minutes












4















How can I delete all Eloquent Records in a database where the created_at or updated_at field is older than 60 minutes? (or any amount of time for the example)



edit: posted solution as answer as @Alexxus suggested










share|improve this question




















  • 2





    possible duplicate of Laravel select records older than 5 minutes?

    – ceejayoz
    Jun 16 '14 at 18:38











  • I would start by learning the MySQL date/time functions dev.mysql.com/doc/refman/5.5/en/….

    – Dave
    Jun 16 '14 at 18:48











  • Please post the aswer as answer

    – Alexxus
    Oct 30 '18 at 6:16
















4















How can I delete all Eloquent Records in a database where the created_at or updated_at field is older than 60 minutes? (or any amount of time for the example)



edit: posted solution as answer as @Alexxus suggested










share|improve this question




















  • 2





    possible duplicate of Laravel select records older than 5 minutes?

    – ceejayoz
    Jun 16 '14 at 18:38











  • I would start by learning the MySQL date/time functions dev.mysql.com/doc/refman/5.5/en/….

    – Dave
    Jun 16 '14 at 18:48











  • Please post the aswer as answer

    – Alexxus
    Oct 30 '18 at 6:16














4












4








4


3






How can I delete all Eloquent Records in a database where the created_at or updated_at field is older than 60 minutes? (or any amount of time for the example)



edit: posted solution as answer as @Alexxus suggested










share|improve this question
















How can I delete all Eloquent Records in a database where the created_at or updated_at field is older than 60 minutes? (or any amount of time for the example)



edit: posted solution as answer as @Alexxus suggested







database laravel-4 eloquent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 14:06







Barry127

















asked Jun 16 '14 at 18:29









Barry127Barry127

7731619




7731619








  • 2





    possible duplicate of Laravel select records older than 5 minutes?

    – ceejayoz
    Jun 16 '14 at 18:38











  • I would start by learning the MySQL date/time functions dev.mysql.com/doc/refman/5.5/en/….

    – Dave
    Jun 16 '14 at 18:48











  • Please post the aswer as answer

    – Alexxus
    Oct 30 '18 at 6:16














  • 2





    possible duplicate of Laravel select records older than 5 minutes?

    – ceejayoz
    Jun 16 '14 at 18:38











  • I would start by learning the MySQL date/time functions dev.mysql.com/doc/refman/5.5/en/….

    – Dave
    Jun 16 '14 at 18:48











  • Please post the aswer as answer

    – Alexxus
    Oct 30 '18 at 6:16








2




2





possible duplicate of Laravel select records older than 5 minutes?

– ceejayoz
Jun 16 '14 at 18:38





possible duplicate of Laravel select records older than 5 minutes?

– ceejayoz
Jun 16 '14 at 18:38













I would start by learning the MySQL date/time functions dev.mysql.com/doc/refman/5.5/en/….

– Dave
Jun 16 '14 at 18:48





I would start by learning the MySQL date/time functions dev.mysql.com/doc/refman/5.5/en/….

– Dave
Jun 16 '14 at 18:48













Please post the aswer as answer

– Alexxus
Oct 30 '18 at 6:16





Please post the aswer as answer

– Alexxus
Oct 30 '18 at 6:16












1 Answer
1






active

oldest

votes


















0














Solved -> With ceejayoz his link




  • Make a date object and minus the amount of time.

  • Covert the date object to string to match Eloquent formatting

  • Compare formatted string against updated_at


I'm using the following code:



$date = new DateTime;
$date->modify('-60 minutes');
$formatted = $date->format('Y-m-d H:i:s');
MyModel::where('updated_at', '<=', $formatted)->delete();


Thanks ceejayoz!






share|improve this answer























    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%2f24249923%2fdelete-elequent-records-older-than-60-minutes%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









    0














    Solved -> With ceejayoz his link




    • Make a date object and minus the amount of time.

    • Covert the date object to string to match Eloquent formatting

    • Compare formatted string against updated_at


    I'm using the following code:



    $date = new DateTime;
    $date->modify('-60 minutes');
    $formatted = $date->format('Y-m-d H:i:s');
    MyModel::where('updated_at', '<=', $formatted)->delete();


    Thanks ceejayoz!






    share|improve this answer




























      0














      Solved -> With ceejayoz his link




      • Make a date object and minus the amount of time.

      • Covert the date object to string to match Eloquent formatting

      • Compare formatted string against updated_at


      I'm using the following code:



      $date = new DateTime;
      $date->modify('-60 minutes');
      $formatted = $date->format('Y-m-d H:i:s');
      MyModel::where('updated_at', '<=', $formatted)->delete();


      Thanks ceejayoz!






      share|improve this answer


























        0












        0








        0







        Solved -> With ceejayoz his link




        • Make a date object and minus the amount of time.

        • Covert the date object to string to match Eloquent formatting

        • Compare formatted string against updated_at


        I'm using the following code:



        $date = new DateTime;
        $date->modify('-60 minutes');
        $formatted = $date->format('Y-m-d H:i:s');
        MyModel::where('updated_at', '<=', $formatted)->delete();


        Thanks ceejayoz!






        share|improve this answer













        Solved -> With ceejayoz his link




        • Make a date object and minus the amount of time.

        • Covert the date object to string to match Eloquent formatting

        • Compare formatted string against updated_at


        I'm using the following code:



        $date = new DateTime;
        $date->modify('-60 minutes');
        $formatted = $date->format('Y-m-d H:i:s');
        MyModel::where('updated_at', '<=', $formatted)->delete();


        Thanks ceejayoz!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 '18 at 14:05









        Barry127Barry127

        7731619




        7731619






























            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%2f24249923%2fdelete-elequent-records-older-than-60-minutes%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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud