Filtering a list of posts in Laravel using the whereBetween method

I have a list of posts with a "created_at" column. I am looking to only retrieve posts that are within 10 minutes old based on their creation time. I have come across many examples of using whereBetween, but they all involve passing values through inputs or hardcoding them.

Is there a way, aside from looping through my query for each post and adding an if statement, to write something like this in Laravel:

$posts->whereBetween('created_at', [$now_date, $current_post_created_at]);

Where the first parameter is "now", and the second parameter is the value of the created_at column for the current post being retrieved from the database.

Answer №1

Attention all future readers of this question, behold the correct answer (I initially had the wrong approach):

Post::whereBetween('created_at', [now()->subMinutes(10), now()])

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to include input text along with a file upload in an AJAX request to upload.php? And how exactly would I go

How do I include the book name entered in the form field with id:bname in the request to be sent to the page upload.php, and how can I retrieve this text on the upload.php page? function uploadFile(){ var file = document.getElementById("upload"). ...

Issues with PHP login functionality

I have been struggling with this issue since last night, so I finally decided to reach out to ask for your help. Can anyone guide me on how to solve this problem? Are there any mistakes in the code provided below? I'm fairly new to PHP and would appr ...

Enhancing WooCommerce checkout experience with personalized AJAX updates beyond the standard order review table

I have customized my WooCommerce checkout page to display a notification before the order review section based on the cart total. In order to achieve this, I added the following code snippet in the functions.php file of my child theme: function action_wooc ...

Can I use AJAX to load an entire PHP file?

My goal is to trigger a PHP file for sending an email when the countdown I created reaches zero. The PHP code contains the email message and does not involve any UI elements. I suspect that my approach may be incorrect, especially since I am not very fami ...

PHP Regular Expression - Identify a single word or a word containing a hyphen

Looking for assistance with implementing preg_match to validate a form. The regular expression should identify inputs that do not consist solely of characters, as well as words containing a hyphen followed by more characters. To clarify, the regex should ...

What is the proper way to access an array in Smarty?

The array called $chapter_theory_details is structured as follows: Array ( [cs_class_id] => 2 [cs_subject_id] => 8 [chapter_id] => 103 [chapter_cs_map_id] => 81 [chapter_title] => Chemistry [chapter_data] => Every ...

Tips for finding two digits using preg_match?

I recently implemented a preg_match to validate if the starting digits were 9477, which worked perfectly. However, I now need to add an additional condition where either 9477 or 9476 should be considered valid. The criteria are as follows: It must consi ...

What could be causing the issue with sending data via Ajax when using the `type="password"` input?

Why does sending data using ajax with input type="password" not work? The main idea is that when I fill in a password less than 6 characters, it should display Password minimum 6 characters I tested this code and found that it was not working. So, I edit ...

Steps to create a personalized value for a column in a MySQL query

During the exam, I am dealing with 2 tables. 1) Post - and its columns : pid : uid : title : content : created date : ... 2) URL alias aid : sou_url : des_url : The purpose of the second table is to store URL aliases for all pages on my website. I have ...

What is the best way to calculate the values of two variables in separate functions and then output the result in another function using PHP?

Is there a way to combine two variables from different functions and store the result in another variable within yet another function? My current code is giving me an output of 0 instead of 30. <?php class race { var $a; var $b; function ...

How can I adjust the format of a timestamp field from the database to adhere to Dutch locale conventions?

I am attempting to convert a timestamp string from the database into a Dutch locale format that resembles: Maandag 2 juli 2013 My approach was as follows: <?php setlocale(LC_ALL, 'nl_NL'); echo strftime("%A %e %B %Y", mktime($vac->gepl ...

Unable to retrieve file from Alias directory using the download script

Currently, I am developing a download script. The PHP files are stored in the xampp htdocs directory, while the folder containing the actual files is on an external HDD. An Alias has been configured for the external drive: <Directory "h:/Filme"> Op ...

show information retrieved from database according to the drop-down menu selection

Can anyone assist me with this query? I have a drop-down menu that includes "Shop A" and "Shop B". Shop A has a value of 1, and Shop B has a value of 2. When I select Shop A from the dropdown, I want to display the data from the database as a table specifi ...

Guide on how to confirm the successful sending of an email or track errors using Codeigniter AJAX

Is there a way to return a value back to AJAX from the controller once the email has been sent? Here is some code: In the following code, I would like to store flash data in the session and have it set under specific conditions to be called back in AJAX. ...

Phalcon: Gain Control of PhalconDebug's Output in Development/Production Environment

I have a small application built on a specific framework. I want to have full control over the user interface when my application encounters an error. Rather than relying on different web browsers' default error responses, I would like to utilize Falc ...

The encryption method of PHP XOR falls short

I have experimented with various solutions to implement XOR encoding on an uploaded file, but it consistently fails. The issue lies in the fact that the unencrypted file uploads smoothly to the MySQL database: $fp = fopen($tempPath, 'r'); $ ...

A step-by-step guide on incorporating universal CSRF tokens using JQuery AJAX

As part of my development work, I am in the process of creating jquery code that communicates with the server through ajax to input data into a database based on specific request parameters. My main concern at this point is the vulnerability to CSRF attac ...

Executing PHP code from a list item

On one of my website pages, I have a list that functions as a delete button. However, I am interested in making it so that when a user clicks on the delete option, a php script is triggered - similar to how a submit button works. Below is the list structu ...

Proceed with another ajax request only when the previous one has been successfully completed and loaded

While scrolling down and loading content into my page, I am facing an issue. The ajax executions load too quickly, causing the subsequent calls to not receive correct information from the first ajax call that is loaded into the DOM. How can I ensure that ...

Guide to integrating custom fields in Wordpress with mapbox-gl js to generate map markers

Currently, I am in the process of generating a map that will display various points using Mapbox and WordPress. To achieve this, I have set up a custom post type within WordPress where the coordinates are stored in custom meta fields. Although the fields h ...