简体   繁体   English

Laravel 4:添加数据库记录并重定向到具有新记录ID的Edit视图

[英]Laravel 4: Add Database Record and Redirect to Edit View with New Record ID

Brand new to Laravel! 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. 我有一个名为PlannerController的控制器。 Within this controller I have the standard store() and edit($id) methods. 在此控制器中,我具有标准的store()和edit($ id)方法。 Here is the code for my store() method: 这是我的store()方法的代码:

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. 当我重定向到planner.edit视图(edit($ id)控制器方法)时,我无法弄清楚如何继承fin_income.id字段,该字段在添加到数据库后会自动递增。 When I do the redirect like this, my redirect url is /planner/{planner}/edit. 当我这样进行重定向时,我的重定向URL是/ planner / {planner} / edit。

Obviously I need something extra in the store() method but what is the best way to go about this? 显然,在store()方法中我需要一些额外的东西,但是最好的方法是什么?

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. 在模型上save()后,将在主键属性上设置自动递增的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM