Orm Laravel雄辩的最后插入对象错误属性

Orm Laravel雄辩的最后插入对象错误属性,orm,insert,laravel-4,eloquent,Orm,Insert,Laravel 4,Eloquent,我在使用Laravel雄辩的ORM时遇到了一个问题: 在数据库中插入新的雄辩模型时,数据已损坏。 具体而言: $newItem = new NotificationNewItem; $newItem->item_id = $item->id; // item_id is the primary key (returned by getKeyName()) $newItem->save(); return NotificationNewItem::find($item->i

我在使用Laravel雄辩的ORM时遇到了一个问题: 在数据库中插入新的雄辩模型时,数据已损坏。 具体而言:

$newItem = new NotificationNewItem;
$newItem->item_id = $item->id; // item_id is the primary key (returned by getKeyName())
$newItem->save();
return NotificationNewItem::find($item->id);
此代码返回的结果与

$newItem = new NotificationNewItem;
$newItem->item_id = $item->id;
$newItem->save();
return $newItem;
这两个项目应该是一样的,不是吗? 奇怪的是,在第一种情况下,返回的JSON对象(我直接在浏览器中显示)正是插入到数据库中的对象,而在第二种情况下,JSON对象的主键(此处为item_id)等于0,即使在数据库中对应的条目的主键等于3(或不同的值)

如果您想再次看到该错误,以下是laravel代码: 在模型函数insertAndGetElement()中有两个“返回”,这些返回项具有不同的主键(pastebin中的第一个返回的主键等于0)

非常感谢您的帮助。 提前感谢,,
罗宾

我不知道您到底想要什么,但要获取最后一个插入id,请使用以下命令:

$newItem = new NotificationNewItem;
$newItem->item_id = $item->id;
$newItem->save();
// $newItem->id; this is lastinsertedid
return $newItem->id;
//NotificationNewItem::find($newItem->id);

该问题的解决方案(主键在调用save()后设置为0)是将模型精确定义为不自动递增主键。 要做到这一点,只需使用

public $incrementing = false;

在模型声明中。感谢安德烈亚斯卢特罗对#拉雷维尔

这不是我的问题:我希望返回刚刚插入的项,但问题是它的一个属性(主键)似乎已损坏(数据不是它应该是的:return$newItem;返回一个item_id等于0但应该不同的项)。这更清楚吗?