Search code examples
laravellaravel-routinglaravel-controller

return redirect()->route with request not parameter in laravel?


I have a function with the name of Direction which takes the request and redirects to the following view if the condition is true. here is the code

   public function direction(Request $request)
{
    $activity_id=$request->id;
    $activity_type=Activity::where('id',$activity_id)->pluck('type');
    if($activity_type[0]=='event')
     {

     return redirect()->route('getActivity',compact('activity_id'));
     }
    
    else{
         echo "please select valid id ";
     }
}

it works perfectly, but I like to use the same function after saving some data in another table. and redirect to the following I use

return  redirect()->route('progress.direction',$activity_id)->with('message','Save Successfully!');

it redirects the only parameter http://localhost:8000/progress/direction?1ac27656-0da2-4dc7-a3f9-3604e002db91 which not get as request. my question is how to send the following parameter as a request from the controller.


Solution

  • You have to modify your routes like below

    Route::any('/progress/direction/{direction?}','ProgressReportController@direction')->name('progress.direction');
    

    here i have added question mark it means its optional .if you not needed optional then you can remove question mark