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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名