morilog / jalali

This Package helps developers to easily work with Jalali (Shamsi or Iranian) dates in php appliations, based on Jalali (Shamsi) DateTime class.
MIT License
852 stars 107 forks source link

empty object return for Jalalian::forge($expire_date); #159

Closed danielhr2001 closed 1 year ago

danielhr2001 commented 1 year ago

I want to convert $expire_date to Jalali date like this:

$account_histories = AccountHistory::where('user_id', Auth::id())->with('accountCategory')->get();
foreach ($account_histories as $account_history) {
$account_history->expire_date = Jalalian::forge($expire_date);
return $account_histories;

but I got this response: reponse:

[
    {
        "id": 1,
        "expire_date": {},
         something...
        "account_category": {
            something...
        }
    },
    {
        "id": 2,
        "expire_date": {},
         something...
        "account_category": {
           something...
        }
    }
]

but when I return return $account_history->expire_date directly, I take correct data 1402-12-03 00:00:00

morilog commented 1 year ago

Because its an object and an object should implements __toString() function to format correctly in direct json marshalling

danielhr2001 commented 1 year ago

thanks it works:

$account_histories = AccountHistory::where('user_id', Auth::id())->with('accountCategory')->get();
foreach ($account_histories as $account_history) {
$account_history->expire_date = Jalalian::forge($expire_date)->__toString();
return $account_histories;

and the result:

[
    {
        "id": 1,
        "expire_date": "1401-12-08 20:22:12",
         something...
        "account_category": {
            something...
        }
    },
    {
        "id": 2,
        "expire_date": "1401-12-08 20:23:17",
         something...
        "account_category": {
           something...
        }
    }
]

آقا دمتگرم کار کرد. انتظار نداشتم جواب یک issu برای پکیج ایرانی اینقدر سریع پاسخ داده بشه. واقعا دمتگرم یک تنه داری کار رو جلو میاری.