保存模型时,Laravel模型尾随数据

时间:2018-05-07 07:17:40

标签: php laravel-5 model trailing invalidargumentexception

我有一些像这样的代码

    $editStuState = StuAtt::where('studentId' , '=' , $id)->first();
    $editStuState -> leave +=1;
    $editStuState -> present = $editStuState -> present-1;
    $editStuState->update();
                            //OR
    $editStuState->save();
    return 'this is good';

我无法保存或更新我的数据, 当我删除更新并保存相关行时,它可以打印文本。

这是dd($editStuState)数据

StuAtt {#382 ▼
  #table: "stu_attendance"
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [▼
    "id" => "7"
    "studentId" => "1"
    "present" => "2"
    "absent" => "1"
    "leave" => "10"
    "created_at" => "2018-04-16 11:17:41.176898"
    "updated_at" => "2018-04-16 06:47:41.000000"
  ]
  #original: array:7 [▼
    "id" => "7"
    "studentId" => "1"
    "present" => "2"
    "absent" => "1"
    "leave" => "10"
    "created_at" => "2018-04-16 11:17:41.176898"
    "updated_at" => "2018-04-16 06:47:41.000000"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

我也从laravel 5.6中得到了这个错误

InvalidArgumentException
Trailing data

6 个答案:

答案 0 :(得分:9)

尾随数据是碳误差,因为你可能使用Postgres而你的日期会返回毫秒。

"created_at" => "2018-04-19 07:01:19.929554"

您可以将以下方法添加到(基础)模型中。

public function getDateFormat()
{
     return 'Y-m-d H:i:s.u';
}

如果您在Postgres中使用TIME WITH TIMEZONE,则格式应为:

Y-m-d H:i:s.uO

答案 1 :(得分:1)

更改您的代码:

x <- '<ReportDelivery responsecode="0" responsetext="descriptive text">
  <Terminal isn="DCC000000001" imo="111111111" name="MV Vessel A">
    <Report>
      <DateTime>01/10/2014 15:30:45</DateTime> 
      <Lat>99.9999999</Lat> 
      <Lon>999.9999999</Lon> 
      <Cog>999</Cog> 
      <Sog>999</Sog> 
      <Voltage>99</Voltage> 
      <Status>Description of status</Status> 
    </Report>
    <Report>
      <DateTime>01/10/2014 15:30:45</DateTime> 
      <Lat>99.9999999</Lat> 
      <Lon>999.9999999</Lon> 
      <Cog>999</Cog> 
      <Sog>999</Sog> 
      <Voltage>99</Voltage> 
      <Status>Description of status</Status> 
    </Report>
  </Terminal>
  <Terminal isn="DCC000000002" imo="222222222" name="MV Vessel B">
    <Report>
      <DateTime>01/10/2014 15:30:45</DateTime> 
      <Lat>99.9999999</Lat> 
      <Lon>999.9999999</Lon> 
      <Cog>999</Cog> 
      <Sog>999</Sog> 
      <Voltage>99</Voltage> 
      <Status>Description of status</Status> 
    </Report>
  </Terminal>
</ReportDelivery>'

要:

$editStuState = StuAtt::where('studentId' , '=' , $id)->first();
$editStuState -> leave +=1;
$editStuState -> present = $editStuState -> present-1;
$editStuState->update();
                        //OR
$editStuState->save();
return 'this is good';

方法 - &gt;更新(...)用于批量更新,请检查Mass Updates

答案 2 :(得分:1)

如果您使用的是Postgres,则必须在模型中添加一些行。发生这种情况的原因是Postgres中的TIME WITH TIMEZONE。

还请阅读Date Mutators,因为Laravel已经支持此功能,只需将其放在模型的下面以覆盖该模型的默认dateFormat:https://laravel.com/docs/5.7/eloquent-mutators#date-mutators

转到您的App / Model(在app文件夹下,exp。User,SomeModel)添加以下行:

protected $dateFormat = 'Y-m-d H:i:sO';

最佳

答案 3 :(得分:0)

如果您的数据库是Postgres,而您的字段是Timestamp,则有时Carbon无法转换为默认格式(毫秒)。

如果不需要毫秒,请将字段内容更新为不包含毫秒部分。

UPDATE YOURTABLE SET created_at = date_trunc('seconds', created_at),
updated_at = date_trunc('seconds', updated_at)

这将规范带时间戳的字段,而无需毫秒。

答案 4 :(得分:0)

我遇到了同样的问题,但是我将列名更改为creation_date,问题解决了。

答案 5 :(得分:0)

在我的情况下,问题是表中created_atupdated_at的长度。在Navicat中,表设计是这样的:

enter image description here

将其更改为0并保存更改:

enter image description here