dongmiyi8220 2014-07-01 20:51
浏览 63
已采纳

Laravel 4:使用新记录ID添加数据库记录并重定向到编辑视图

Brand new to Laravel!

As my title states I would like to be able to immediately redirect from storing a database record to its edit view.

I have a controller called PlannerController. Within this controller I have the standard store() and edit($id) methods. Here is the code for my store() method:

public function store()
{
    $newIncome = new Income('fin_income');
    $newIncome->user_id = '1';
    $newIncome->income_id = Input::get('categories');
    $newIncome->income_desc = "Test YO!";
    $newIncome->income_date = date('Y-m-d');
    $newIncome->amount = Input::get('amount');
    $newIncome->save();

    return Redirect::route('planner.edit');
}

When I redirect to the planner.edit view (the edit($id) controller method) I have not been able to figure out how to carry over the fin_income.id field, which is auto_incremented upon addition to the database. When I do the redirect like this, my redirect url is /planner/{planner}/edit.

Obviously I need something extra in the store() method but what is the best way to go about this?

  • 写回答

1条回答 默认 最新

  • douanrang4728 2014-07-01 22:26
    关注

    have you tried:

    return Redirect::route('planner.edit', ['id' => $newIncome->id]);
    

    after you save() on the model, the auto incremented value will be set on your primary key property.

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

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置