How to Get Last Week Data in CakePHP 4 Tutorial

Reading Time: 3 minutes
1,102 Views

Inside this article we will see how to get last week data in CakePHP 4. Article has very classified information about last week data selection from database in CakePHP 4.

Selecting data rows from database in CakePHP 4, we have several options available as – Using Models, Using Query Builder, Using raw queries.

Last week data selection is very helpful when we are creating data reports of application.

This article gives you a detailed on CakePHP 4 to get Last week data. Here, Creating a basic example of CakePHP from which we will select data in last week.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

Create Database

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE mydatabase;

Successfully, we have created a database.

Database Connection

Open app_local.php file from /config folder. Search for Datasources. Go to default array of it.

You can add your connection details here to connect with your database. It will be like this –

//...

'Datasources' => [
        'default' => [
            'host' => 'localhost',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',

            'username' => 'root',
            'password' => 'sample@123',

            'database' => 'mydatabase',
            /*
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /*
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],
  
     //...

//...

You can pass host, username, password and database.

Successfully, you are now connected with the database.

Sample Database Data Rows

Here, we have taken a sample database to understand this example.

Let’s say we have a users table inside it which contains users data. Please look into this screenshot to get clear vision about table.

Next,

We want to get all data from this table of last week in CakePHP 4.

Create Query for Data

Open any controller from your application.

Let’s add last week concept into it to get data.

<?php

declare(strict_types=1);

namespace App\Controller;

use Cake\Datasource\ConnectionManager;

class SiteController extends AppController
{
    private $db;

    public function initialize(): void
    {
        parent::initialize();

        $this->db = ConnectionManager::get("default");
        $this->autoRender = false;
    }

    public function getUsers()
    {
        $results = $this->db
            ->newQuery()
            ->select('name, email, phone_no')
            ->from('users')
            ->where('created_at BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()')
            ->execute()
            ->fetchAll('assoc');

        print_r($results);
    }
}

Query Concept

Make use of MySQL Last week Data Selection Concept.

Generated Query

SELECT `name`, `email`, `phone_no` FROM `users` WHERE `created_at` BETWEEN CURDATE()-INTERVAL 1 WEEK AND CURDATE()

When we run this code, we will get result of last week.

We hope this article helped you to learn How to Get Last week Data in CakePHP 4 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.