redirect with 2 ids

Published 1 week ago by msyaukat

how do I redirect after updating when my show has 2 ids: company_id and company_branch_id.

route:

Route::get('company/{company_id}/companybranch/{company_branch_id}', ['uses' => '[email protected]', 'as' => 'companybranch.show']);

controller of update:

public function update(Request $request, $company_branch_id)
    {
        $company = Company::find($company_id);
        $companybranch = CompanyBranch::find($company_branch_id);

//didn't include the middle part

 return redirect()->route('companybranch.show', $company->company_id, $companybranch->company_branch_id);
Best Answer (As Selected By msyaukat)
bobbybouwmann

You need to pass an array to the route

 return redirect()->route('companybranch.show', [$company->company_id, $companybranch->company_branch_id]);

Note that this will pass the variables based on their name to the correct parameter in your route, so you should be able to switch them around.

If you however have different variables you can do something like this

 return redirect()->route('companybranch.show', [
    'company_id' => $company->company_id, 
    'company_branch_id' => $companybranch->company_branch_id,
]);
bobbybouwmann

You need to pass an array to the route

 return redirect()->route('companybranch.show', [$company->company_id, $companybranch->company_branch_id]);

Note that this will pass the variables based on their name to the correct parameter in your route, so you should be able to switch them around.

If you however have different variables you can do something like this

 return redirect()->route('companybranch.show', [
    'company_id' => $company->company_id, 
    'company_branch_id' => $companybranch->company_branch_id,
]);
msyaukat

what does this mean?

Missing argument 3 for App\Http\Controllers\CompanyBranchController::update()

bobbybouwmann

Your method has 3 arguments, but you only give it 2 arguments. Maybe you forgot to give it the route parameter?

msyaukat

if I want to pass this:

return redirect()->route('companybranch.show', [
    'company_id' => $company->company_id, 
    'company_branch_id' => $companybranch->company_branch_id,
]);

that means I need to call:

$company = Company::find($company_id);

in my update controller right? when I did this, it says $company is undefined. How?

msyaukat

done I just hv to call one model. tq

Please sign in or create an account to participate in this conversation.