DS87
DS87

Reputation: 602

Laravel redirect to other route

I have a function getLocation, that route is named "search.location" with for example the URL www.url.com/service/ny/slug. This shows a single location.

I now want to check, if the location is available. If the location with the given parameters is not available, I want, to call the route "search.servicecity" that has the parameters service and city. The URL is then www.url.com/service/ny/. How can I achieve to call the route with the parameters?

public function getLocation($servicename, $city, $slug){
  $location = Location::where([
      ['slug','=',$slug],
      ['city','=',$city]
    ])->first();

  if(count($location)>0){
    return view('search.location')->with('servicename',$servicename)
                ->with('city',$city)->with('information','getLocation')
                ->with('slug',$slug)->with('location',$location);
  }else{
    //call search.servicecity with parameters $servicename and $city
  }
}

Upvotes: 0

Views: 35

Answers (1)

fubar
fubar

Reputation: 17378

You can use the redirect helper for this.

return redirect()->route('search.servicecity', [
    'service' => $servicename,
    'city'    => $city,
]);

Check out the docs for other examples.

Upvotes: 1

Related Questions