How do I make global helper functions in laravel

0 votes
If I wanted to make a currentUser() function for some oauth stuff I am doing where I can use it in a view or in a controller (think rails, where you do helper_method: current_user in the application controller).

Everything I read states to create a helpers folder and add the function there and then that way you can do Helpers::functionName Is this the right way to do this?

Whats the "laravel way" of creating helper functions that can be used in blade templates and controllers?
Nov 22, 2020 in PHP by kartik
• 37,520 points
1,360 views

1 answer to this question.

0 votes

Hello @kartik,

Create a new file in your app/Helpers directory name it AnythingHelper.php An example of my helper is :

<?php
function getDomesticCities()
{
$result = \App\Package::where('type', '=', 'domestic')
    ->groupBy('from_city')
    ->get(['from_city']);

return $result;
}

generate a service provider for your helper by following command

php artisan make:provider HelperServiceProvider

in the register function of your newly generated HelperServiceProvider.php add following code

require_once app_path('Helpers/AnythingHelper.php');

now in your config/app.php load this service provider and you are done

'App\Providers\HelperServiceProvider',

Hope it helps!!

answered Nov 22, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
1 answer

How do I make an asynchronous GET request in PHP?

Hello @kartik, Works with both POST and GET ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,840 points
1,848 views
0 votes
1 answer

How do I reload a relation collection in laravel?

You can easily tell laravel to load ...READ MORE

answered Nov 4, 2020 in PHP by anonymous
• 82,840 points
6,877 views
0 votes
1 answer

How do I get all posts from $wp_query in the search results in wordpress?

Hello @kartik, Set posts_per_page parameter in args to ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
6,366 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

Hello @kartik, In SQL Server, use MERGE MERGE INTO YourTable ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,840 points
939 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,698 views
0 votes
1 answer

How do I make a redirect in PHP?

Hello @kartik, Use the header() function to send an HTTP Location header: header('Location: '.$newURL); Contrary to ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,840 points
711 views
0 votes
1 answer

How do I make a PATCH request in PHP using cURL?

Hello @kartik, Use this: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); should do it. Hope ...READ MORE

answered Oct 19, 2020 in PHP by Niroj
• 82,840 points
8,504 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP