2015-07-02 6 views
5

में दिनांक का अप्रत्याशित व्यवहार मैं लार्वेल में एक वेब सेवा कर रहा हूं जो JSON लौटा रहा है। अपेक्षा के अनुरूपलैरवेल: JSON प्रतिक्रिया

class Account extends Eloquent { 

    // The database table used by the model. 
    // (If not defined then lowercase and plural of class name is consider as a table name) 
    protected $table = "account"; 

    // define which column can be mass assign 
    protected $fillable = array("user_id", "account_group_id", "generated_by", "image", "name", 
           "address", "zip", "area_id", "mobile", "email", "phone", "fax", 
           "website", "pan", "cst", "tin", "ecc", "iesc", "transport", 
           "other", "outstanding", "cform", "status", "mitp"); 

    // To prevent column from mass assignment. 
    protected $guarded = array('id'); 

    // Change Variable for CREATED_AT and UPDATED_AT 
    const CREATED_AT = 'itp'; 
    const UPDATED_AT = 'utp'; 
} 

मैं Account से खेतों को लाते समय हूँ user_id का उपयोग कर और मेरे नियंत्रक

$accountData = Account::select('name', 'status', 'id', 'user_id', 'utp')->where('user_id', Auth::id())->first(); 
$return = array(
       'result' => 'success', 
       'msg' => 'Login Successfully.', 
       'data' => $accountData 
       ); 
return Response::json($return); 

इस में, utp बर्ताव में Response::json() के माध्यम से JSON लौटने:

मैं बहुत की तरह एक Account मॉडल बनाया है और एक स्ट्रिंग के रूप में एक तिथि देता है:

{ 
    "result": "success", 
    "msg": "Login Successfully.", 
    "data": { 
    "name": "Demo", 
    "status": 0, 
    "id": 143, 
    "user_id": 207, 
    "utp": "2015-07-01 18:38:01" 
    } 
} 

लेकिन अगर मैं इतना की तरह खाते मॉडल से अलग मान ले:

$return = array(
    'result' => 'success', 
    'msg' => 'Login Successfully.', 
    'data' => $accountData['user_id'], 
    'account_id' => $accountData['id'], 
    'utp' => $accountData['utp'], 
    'usertype' => 'account', 
    'status' => $accountData['status'] 
); 

फिर इस से utp

{ 
    "result": "success", 
    "msg": "Login Successfully.", 
    "data": 207, 
    "account_id": 143, 
    "utp": { 
    "date": "2015-07-01 18:38:01", 
    "timezone_type": 3, 
    "timezone": "Asia\\/Kolkata" 
    }, 
    "usertype": "account", 
    "status": 0 
} 

कुछ अनपेक्षित व्यवहार देता है क्यों यह मेरा टाइमस्टैम्प क्षेत्र के साथ होता है?

उत्तर

5

क्योंकि utp एक Carbon\Carbon उदाहरण है। Model::toJson (वास्तव में Model::toArray, लेकिन दोनों का इस्तेमाल किया जाता है) को संभालती है कि आम तौर पर, और एक तारीख को धारावाहिक यह सामान्य ISO3601-ish प्रारूप

अपेक्षित व्यवहार के लिए है, तो आप कार्बन उदाहरण फ़ॉर्मेट करना होगा।

"utp" => $accountData['utp']->format("Y-m-d H:i:s"), 

वैकल्पिक रूप से, एक स्ट्रिंग

"utp" => (string) $accountData['utp'], 
करने के लिए इसे डाली
संबंधित मुद्दे