【问题标题】:updated_at timestamp not being updated when using sync in Laravel 4在 Laravel 4 中使用同步时,updated_at 时间戳未更新
【发布时间】:2014-07-18 03:53:27
【问题描述】:

我使用以下代码来更新有关组织的标签和其他信息:

Route::put('org/{org}', function(Org $org){
    $org->description = Input::get('description');
    $org->website = Input::get('website');
    $org->save();

    $org->tags()->sync(Input::get('tags'));

    return Redirect::to('org/'.$org->id)
        ->with('message', 'Seccessfully updated page!');
});

但是,如果我只更改与此组织关联的标签,updated_at 字段不会更新。我在我的 Tag 模型中添加了protected $touches = array('org');,但这似乎只适用于 belongsTo 关系,而 orgs 和标签之间的关系是多对多的多态关系。

有没有办法让同步函数自动更新默认的 updated_at 时间戳?

手动操作(如下所示)会更新时间戳,即使我的编辑实际上并没有改变任何内容:

$org->updated_at = \Carbon\Carbon::now()->toDateTimeString();
$org->save();

【问题讨论】:

  • 究竟是哪个时间戳?
  • 已编辑问题。我说的是在保存模型时应该自动维护的默认 updated_at 时间戳。

标签: laravel-4 eloquent polymorphic-associations


【解决方案1】:

您必须在Tag 模型上设置touches 数组,但它有限制:

Eloquent 猜测关系名称,因此只有在名称符合约定时才会起作用。

Organization 模型 -> organizations 关系。

如果你想手动更新时间戳,你可以改用这个:

$model->touch();

【讨论】:

  • 我的组织模型/表格没有按照惯例设置,所以这可能是设置 touches 数组不起作用的原因。我最终手动完成了 - 我只会将您的答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 2019-01-22
  • 2016-08-09
  • 2019-05-06
  • 2013-10-05
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多