Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

grandadevans's avatar

Carbon date not being set

Hi Everyone,

I am having a little trouble with the $dates feature of Eloquent. As you can see from the tinker output I have set the $dates property. When I call

$model->getAttribute('dateFieldName');

I get the correct value (an instance of Carbon) but when I call

$model->dateFieldName;

I get null. I have included some artisan tinker output below to illustrate my point.

3546 $ art tinker
Psy Shell v0.5.2 (PHP 5.5.27 — cli) by Justin Hileman
>>> $med1 = \MPC\Models\Medication::first();
=> MPC\Models\Medication {#703
     id: 1,
     user_id: 1,
     name: "tramadol hydrochloride 50 MG Oral Capsule",
     RXCUI: "836466",
     preferredName: "Tramadol",
     sizeOfEachUnit: null,
     unitOfMeasurement: null,
     howManyTakenPerTime: 2,
     timesTakenPerTimePeriod: 4,
     timePeriodTakenOver: "day",
     unitsPerPrescription: 150,
     datePrescriptionLastFilled: "2015-09-29 00:00:00",
     reminder: 1,
     remarks: "Prescription held at Doctor's as Tramadol is now a controlled drug",
     created_at: "2015-10-14 22:30:33",
     updated_at: "2015-10-14 22:30:33",
   }
>>> $med1->getAttribute('datePrescriptionLastFilled');
=> Carbon\Carbon {#702
     +"date": "2015-09-29 00:00:00.000000",
     +"timezone_type": 3,
     +"timezone": "UTC",
   }
>>> $med1->getAttribute('datePrescriptionLastFilled')->addDays(25);
=> Carbon\Carbon {#706
     +"date": "2015-10-24 00:00:00.000000",
     +"timezone_type": 3,
     +"timezone": "UTC",
   }
>>> $med1->datePrescriptionLastFilled;
=> null
>>> $med1->created_at;
=> Carbon\Carbon {#690
     +"date": "2015-10-14 22:30:33.000000",
     +"timezone_type": 3,
     +"timezone": "UTC",
   }
>>> $med1->getDates();
=> [
     "created_at",
     "updated_at",
     "datePrescriptionLastFilled",
     "created_at",
     "updated_at",
   ]
>>>

The field attribute is set as a timestamp in the migration file (it did begin as a date field and I couldn't get the 'Y-m-d' format to work so I switched to a timestamp format until I have the issue solved. Any help appreciated. Thanks in advance

0 likes
5 replies
Snapey's avatar

ok, go with me on this for a moment. What happens in response to $med1->date_prescription_last_filled; ?

grandadevans's avatar

Hi Snapey,

Thanks for replying. That returns null I'm afraid but I have discovered that all the properties are null and the only way to get them is to use getAttribute on whichever field I want.

This is how I am doing the test that lead to the problem.

<?php

use Illuminate\Foundation\Testing\DatabaseTransactions;

class MedicationTest extends TestCase
{
    use DatabaseTransactions;

    public function setUp()
    {
        parent::setUp();
        factory('MPC\Models\User')->create();
    }


    public function test_we_get_days_of_prescription_remaining()
    {
        \MPC\Models\Medication::create([
            'user_id' => 1,
            'howManyTakenPerTime' => 2,
            'timesTakenPerTimePeriod' => 4,
            'timePeriodTakenOver' => 'day',
            'unitsPerPrescription' => 200,
            'datePrescriptionLastFilled' => '2015-01-01 00:00:00',
            'name' => 'tramadol hydrochloride',
            'preferredName' => 'Tramadol'
        ]);

        dd(\MPC\Models\Medication::first());
    }
    
}

and this is the output for dd()

PHPUnit 4.8.12 by Sebastian Bergmann and contributors.

.MPC\Models\Medication {#178
  +howManyTakenPerTime: null
  +timesTakenPerTimePeriod: null
  +timePeriodTakenOver: null
  +unitsPerPrescription: null
  +datePrescriptionLastFilled: null
  #table: "medication"
  #fillable: array:13 [
    0 => "user_id"
    1 => "name"
    2 => "RXCUI"
    3 => "preferredName"
    4 => "sizeOfEachUnit"
    5 => "unitOfMeasurement"
    6 => "howManyTakenPerTime"
    7 => "timesTakenPerTimePeriod"
    8 => "timePeriodTakenOver"
    9 => "unitsPerPrescription"
    10 => "datePrescriptionLastFilled"
    11 => "reminder"
    12 => "remarks"
  ]
  #dates: array:3 [
    0 => "created_at"
    1 => "updated_at"
    2 => "datePrescriptionLastFilled"
  ]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:16 [
    "id" => 1
    "user_id" => 1
    "name" => "tramadol hydrochloride"
    "RXCUI" => null
    "preferredName" => "Tramadol"
    "sizeOfEachUnit" => null
    "unitOfMeasurement" => null
    "howManyTakenPerTime" => 2
    "timesTakenPerTimePeriod" => 4
    "timePeriodTakenOver" => "day"
    "unitsPerPrescription" => 200
    "datePrescriptionLastFilled" => "2015-01-01 00:00:00"
    "reminder" => null
    "remarks" => ""
    "created_at" => "2015-10-14 23:48:02"
    "updated_at" => "2015-10-14 23:48:02"
  ]
  #original: array:16 [
    "id" => 1
    "user_id" => 1
    "name" => "tramadol hydrochloride"
    "RXCUI" => null
    "preferredName" => "Tramadol"
    "sizeOfEachUnit" => null
    "unitOfMeasurement" => null
    "howManyTakenPerTime" => 2
    "timesTakenPerTimePeriod" => 4
    "timePeriodTakenOver" => "day"
    "unitsPerPrescription" => 200
    "datePrescriptionLastFilled" => "2015-01-01 00:00:00"
    "reminder" => null
    "remarks" => ""
    "created_at" => "2015-10-14 23:48:02"
    "updated_at" => "2015-10-14 23:48:02"
  ]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [
    0 => "*"
  ]
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}
grandadevans's avatar

OK: this is just weird. When I alter the test method body to

        \MPC\Models\Medication::create([
            'user_id' => 1,                                        // 1
            'howManyTakenPerTime' => 2,                            // null
            'timesTakenPerTimePeriod' => 4,                        // null
            'timePeriodTakenOver' => 'day',                        // null
            'unitsPerPrescription' => 200,                         // null
            'datePrescriptionLastFilled' => '2015-01-01 00:00:00', // null
            'name' => 'tramadol hydrochloride',                    // tramadol hydrochloride
            'preferredName' => 'Tramadol'                          // Tramadol
        ]);
        dd(\MPC\Models\Medication::first()->preferredName);

I change the field in the dd() call one at a time and I have logged the output in a comment after the field (above). AS you can see there was only user_id, name and preferredName that gave the correct answer EVERY field returned the correct information when wrapped in getAttribute('')

grandadevans's avatar

I have just returned to the code and thanks to the below code I have discovered that the only way to set the attributes is with setAttribute();

    public function test_we_get_days_of_prescription_remaining()
    {
        $newMed = \MPC\Models\Medication::create([
            'user_id' => 1,                                        // 1
            'howManyTakenPerTime' => 2,                            // null
            'timesTakenPerTimePeriod' => 4,                        // null
            'timePeriodTakenOver' => 'day',                        // null
            'unitsPerPrescription' => 200,                         // null
            'datePrescriptionLastFilled' => '2015-01-01 00:00:00', // null
            'name' => 'tramadol hydrochloride',                    // tramadol hydrochloride
            'preferredName' => 'Tramadol'                          // Tramadol
        ]);
        $this->assertInstanceOf(\Carbon\Carbon::class, $newMed->getAttribute('datePrescriptionLastFilled'));
        $this->assertSame(25, $newMed->getLengthOfPrescriptionInDays());

    /**
     * Fails
     *
     * $newMed->timePeriodTakenOver = 'hour';
     * $newMed->save();
         */

    /**
     * Passes
     */
    $newMed->setAttribute('timePeriodTakenOver', 'hour');
        $newMed->save();

        $this->assertSame((25/24), $newMed->getLengthOfPrescriptionInDays());
    }
grandadevans's avatar
grandadevans
OP
Best Answer
Level 4

Ok. I've solved this one. The problem was not only with the date attribute but every attribute of the model. The problem was that I declared the properties in the model. Without going through the source code I am not sure why but I noticed that some of the properties (the original ones) were not there but the newest ones worked as expected.

Please or to participate in this conversation.