Answer the question
In order to leave comments, you need to log in
How to get rid of repetitions in Laravel 8 code?
Tell me, how to get rid of repetitions of such code in each function of the controller?
public function home(Request $request)
{
$agent = new Agent();
$device = $agent->device();
$viewer = New Viewers();
$ip = $request->ip();
$data = Location::get($ip);
$viewer->request_uri = $request->getRequestUri();
$viewer->ip_address = $ip;
$viewer->country = $data->countryName;
$viewer->city = $data->cityName;
if($agent->isMobile()){
$phone = $agent->isPhone();
$viewer->device = $agent->platform();
} else if($agent->isTablet()) {
$viewer->device = $agent->device();
} else if($agent->isRobot()) {
$robot = $agent->robot();
$viewer->device = $robot;
} else {
$platform = $agent->platform();
$viewer->device = $platform;
}
$viewer->save();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question