Home > Software engineering >  Laravel 9 how to pass parameter in url from one controller to another controller?
Laravel 9 how to pass parameter in url from one controller to another controller?

Time:09-04

I was facing this problem of missing parameter when trying to pass a parameter from one controller to another controller. The parameter is $id whereby the data is originally from post method in details blade.php into function postCreateStepOne However, I want to pass the data into a new view and I return

redirect()->route('details.tenant.step.two')->with( ['id' => $id] );}

And this is where error occur. However, it works fine if I skip it into a new route and directly return into a view with the compact parameter. For Example,

return view('document.details-step-two', compact('id', 'property'));

However, I would prefer a new url as I was doing multistep form using Laravel.

Error enter image description here

web.php

Route::get('/document/details/viewing/{id}', 'ViewDetails')->name('details.tenant');
Route::post('/document/details/viewing/{id}', 'postCreateStepOne')->name('post.step-one');
Route::get('/document/details/viewing/step-2/{id}', 'ViewDetailsStep2')->name('details.tenant.step.two');

TenanatController.php

public function viewDetails($id){

        $view = Properties::findOrFail($id);
        return view('document.details', compact('view'));
    }

public function ViewDetailsStep2(Request $request, $id){

        $view = Properties::findOrFail($id);
        $property = $request->session()->get('property');

        return view('document.details-step-two', compact('view', 'property'));
    }

public function postCreateStepOne($id, Request $request)
    {
        $validatedData = $request->validate([
            'property-name' => 'required',
        ]);
  
        if(empty($request->session()->get('property'))){
            $property = new Tenancy();
            $property->fill($validatedData);
            $request->session()->put('property', $property);
        }else{
            $property = $request->session()->get('property');
            $property->fill($validatedData);
            $request->session()->put('property', $property);
        }
  
        return  redirect()->route('details.tenant.step.two')->with( ['id' => $id] );
    }

details.blade.php

<form action="{{ route('post.step-one', $view->id) }}" method="POST">
                @csrf

                <div >
                    <div >
                        <label for="title">Property Name:</label>
                        <input type="text" value=""  id="property-name"  name="property-name">
                    </div>
                </div>

                <div >
                    <button type="submit" >Next</button>
                </div>
</form>

CodePudding user response:

When you use with on a redirect the parameter is passed through the session. If you want to redirect to a route with a given route parameter you should pass that parameter in the route function itself like e.g.

return  redirect()->route('details.tenant.step.two', ['id' => $id]);
  • Related