dongxuan58311366668 2015-03-24 20:57
浏览 881
已采纳

在Laravel中保存后返回主要ID

I am trying to learn Laravel and struggling on how to return an id after I've inserted a row into the database.

Here is my current NotesController:

    public function store()
{
    $form = new NotesCreation($this->currentUser(), $this->params());
    if ($form->save()) {
        return Redirect::route('notes.edit', WHERE ID NEEDS TO GO)
            ->withSuccess('Note added successfully');
    } else {
        $this->view('create')
            ->withErrors($form->getErrors());
    }
}

I have tried to add $form->id, but it does not work. I get an Undefined property: NotesCreation::$id

This is my NotesCreation model:

    public function save()
{
    $success = false;

    if ($this->isValid()) {
        $this->user->notes()->save($this->notes);

        $success = (bool) $this->notes;
    }

    return $success;
}

What am I doing wrong? I greatly appreciate any help! Thanks!

  • 写回答

1条回答 默认 最新

  • doushe2513 2015-04-15 21:39
    关注

    Your ->notes()->save() method must return the id:

    $success = $this->user->notes()->save($this->notes);
    return $success;
    

    You can read about how to return last intested id in: http://laravel.com/docs/4.2/eloquent#insert-update-delete or http://laravel.com/docs/4.2/queries#inserts

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?