Mar*_*łek 16 php laravel eloquent laravel-4
我有以下代码:
$entry->save();
$categories = [];
$categories[Input::get('main_category')] = ['main' => 1];
for ($i=1; $i<=4 ; ++$i) {
$input = Input::get('category_'.$i);
if ($input != '') {
$categories[$input] = ['main' => 0];
}
}
$entry->categories()->sync($categories);
$inTags = explode(',', trim( Input::get('tags'), ','));
$tags = [];
foreach ($inTags as $tag) {
$tag = trim($tag);
if ($tag == '') {
continue;
}
$fTag = Tag::firstOrCreate(array('name' => $tag));
$tags[$fTag->id] = ['entry_id' => $entry->id];
}
$entry->tags()->sync($tags);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中我创建了一个条目($entry->save()这里只是理解它,早期的代码并不重要),然后保存到这个条目所属的类别(使用数据透视表)并对标记执行相同的操作(但是如果标记没有,则为标记.存在我创建一个).
但是在两个数据透视表中,created_at字段0000-00-00 00:00:00在插入数据后保留为default()(可能与之相同,updated_at但我没有对其进行测试).
我是否需要以某种方式自动激活归档时间戳或者需要我自己手动填写它们(但可能意味着更多编码而不使用sync)?
Jar*_*zyk 52
为了在同步/附加belongsToMany关系时处理数据透视时间戳,您需要withTimestamps()在关系定义中包含:
// some model
public function someRelation()
{
return $this->belongsToMany('RelatedModel')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)
然后每个sync/ attach/ updateExistingPivot将设置/更新数据透视表上的时间戳.
| 归档时间: |
|
| 查看次数: |
8089 次 |
| 最近记录: |