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

在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 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误