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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退