Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel:JSON响应中日期的意外行为_Laravel_Laravel 4 - Fatal编程技术网

Laravel:JSON响应中日期的意外行为

Laravel:JSON响应中日期的意外行为,laravel,laravel-4,Laravel,Laravel 4,我正在用Laravel制作一个返回JSON的web服务 我创建了一个帐户模型,如下所示: 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";

我正在用Laravel制作一个返回JSON的web服务

我创建了一个
帐户
模型,如下所示:

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';
}
$return = array(
    'result' => 'success',
    'msg' => 'Login Successfully.',
    'data' => $accountData['user_id'],
    'account_id' => $accountData['id'],
    'utp' => $accountData['utp'],
    'usertype' => 'account',
    'status' => $accountData['status']
);
我使用
user\u id
Account
获取字段,并通过控制器中的
Response::JSON()
返回JSON

$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
的行为符合预期,并以字符串形式返回日期:

{
  "result": "success",
  "msg": "Login Successfully.",
  "data": {
    "name": "Demo",
    "status": 0,
    "id": 143,
    "user_id": 207,
    "utp": "2015-07-01 18:38:01"
  }
}
但是,如果我将每个值与帐户模型分开,如下所示:

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';
}
$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
}

为什么我的时间戳字段会出现这种情况?

因为
utp
是一个实例
Model::toJson
(实际上是
Model::toArray
,但两者都被使用)通常处理该问题,并将日期序列化为通常的ISO3601 ish格式

对于预期的行为,您需要格式化Carbon实例

"utp" => $accountData['utp']->format("Y-m-d H:i:s"),
或者,将其转换为字符串

"utp" => (string) $accountData['utp'],