2014-07-01 20:51
浏览 62

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');

    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 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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题