I am having trouble to make a query which works on all pages, like to get ip address of user on each page of website they visit with url. As of now i am putting this query on each controller.
**controller:**
public function index(Request $request)
{
$ip = $request->ip();
$userDetail = Location::get($ip);
$visitor = new Visitor;
$visitor->ip = $userDetail->ip;
$visitor->url = url()->full();
$visitor->save();
return view('index');
}
CodePudding user response:
You should create a service provider. This will run on every invocation of your application.
Within that service provider, you'll run the query and use View::share
to share it to all Blade views.
CodePudding user response:
you can create a helper file like: helper.php and set the path in composer.json in autoload part like this:
"files": [
"app/Helpers/helpers.php"
],
then inside helprt.php create your function like : myfunction() and use it in all of your project.