简体   繁体   中英

Laravel 4: Add Database Record and Redirect to Edit View with New Record 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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