dimanche 3 novembre 2019

Using where condition inside collection filter method - Laravel

I have a collection that I want to filter if its property value falls under the current month. For that, I am using whereBetween method inside collection filter method but its not filtering any item and returning the whole collection as it is.

I am using Laravel 5.8

Here is my code

Carbon date

public $c_som, $c_eom

$this->c_som = Carbon::now('Asia/Kolkata')->startOfMonth();
$this->c_eom = Carbon::now('Asia/Kolkata')->endOfMonth();

Collection

"cm_revenue_data": [
        {
            "candidate_full_name": "Dr. Joanne Cruickshank",
            "revenue": 1777,
            "doj": "2019-11-16",
            "is_claw_back": 1,
            "claw_back_date": "2019-10-25"
        },
        {
            "candidate_full_name": "Mack Green",
            "revenue": 2718,
            "doj": "2019-07-28",
            "is_claw_back": 1,
            "claw_back_date": "2019-10-25"
        },
        {
            "candidate_full_name": "Mrs. Mireille Beer MD",
            "revenue": 1118,
            "doj": "2019-06-18",
            "is_claw_back": 1,
            "claw_back_date": "2019-10-25"
        },
        {
            "candidate_full_name": "Phoebe Quigley",
            "revenue": 1655,
            "doj": "2019-10-10",
            "is_claw_back": 1,
            "claw_back_date": "2019-11-13"
        },
        {
            "candidate_full_name": "Johnpaul Prosacco",
            "revenue": 1312,
            "doj": "2019-07-12",
            "is_claw_back": 1,
            "claw_back_date": "2019-10-25"
        },
        {
            "candidate_full_name": "Alene Moore DVM",
            "revenue": 2059,
            "doj": "2019-10-11",
            "is_claw_back": 0,
            "claw_back_date": null
        },
        {
            "candidate_full_name": "Vida Fisher",
            "revenue": 1955,
            "doj": "2019-09-07",
            "is_claw_back": 1,
            "claw_back_date": "2019-10-25"
        }
]

Filter Method

$cm_revenue_data = $revenue_data->filter(function ($item) {

            return $item
                ->whereBetween('doj', [$this->c_som, $this->c_eom]);
                ->orWhereBetween('claw_back_date', [$this->c_som, $this->c_eom]);
        });


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire