I have a method in Laravel controller. How can I improve this code? How can I implement single responsibilty here or maybe there are some other tips I can use in this code? I know that controller methods should be kept as small as possible,but I don't know if it's better idea to split this code in separate methods.
public function displayData(CityRequest $request)
{
if (isset($request->validator) && $request->validator->fails()) {
return redirect()->back()->withErrors($request->validator->messages());
}
else {
$cityName = $request->city;
$cityName = str_replace(' ','-',$cityName);
$cityName = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $cityName);
$response = $this->guzzle->request('GET', asset("/api/products/recommended/" . $cityName));
$response_body = json_decode($response->getBody());
if(isset($response_body->error))
{
return redirect()->back()->withErrors(['city'=>$response_body->error]);
}
$city = $response_body->city;
$condition = $response_body->current_weather;
$products = $response_body->recommended_products;
return back()->with(['city' => $city, 'condition' => $condition, 'products' => $products]);
}
}