I am building an app where each company have multiple users. And all users can upload documents/images/xls etc. I want to keep all company data in company separate folder. To complete this I am checking the company detail for every user and then upload data to company specific folder. Can I check company database once per user login and share user's company details to all controller and can easily access.
-
You can make the helper function for it. that function you can access in blade file, controller as well route file. – Dilip Hirapara Oct 23 '19 at 05:34
-
Your question is a bit confusing, are you asking how do you share it to all controllers or all views? – Michael Mano Oct 23 '19 at 05:36
-
@MichaelMano I am trying to share the company details (it have folder name) to all controllers and all views as well. thanks!! – Govind M Oct 23 '19 at 05:50
3 Answers
Use view composer in your AppServiceProvider
App\Providers\AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('*',function($view) {
if(auth()->user()) {
$comanyData = App\Company::where('user_id',auth()->user()->id);
$view->with('companyData', $companyData);
}
});
}
}

- 32,064
- 4
- 39
- 55
-
Thank you. But I need to pass company data from database for every user once he/she is logged in. I will check company detail for that user and get the data for him/her and pass it to all controllers and views. Any more ideas can help me a lot.. – Govind M Oct 23 '19 at 07:09
-
this should work, you just need to check if the user is logged in and then do a query – madalinivascu Oct 23 '19 at 07:13
-
-
Create a class that extends the Controller class, create a property/function with your desired value, extend all your controller from that class, or edit the Controller class and create your desired property/function – madalinivascu Oct 23 '19 at 08:39
-
or you can use `view()->shared('companyData')` to get the shared value – madalinivascu Oct 23 '19 at 08:43
You can make the helper function to use in controllers or blades files.
Let’s create a helper!
- Create a simple PHP file.
Create Helper.php inside the app directory or wherever directory you want.
<?php
/**
* get company data
*
*/
function companyData()
{
// Create logic of company data
// return company data
}
- Autoload Composer
After we created our helper, Laravel won’t recognize our file so we need to register the helper file to our composer.json. Add File array inside the autoload section. It may look like this:
"autoload": {
"classmap": ["database"],
"psr-4": {"App\\": "app/"},
"files" : ["app/Helper.php"]
}
Then don’t forget to run
composer dumpautoload
- Using helper function
Our helper is autoloaded now, so we should be able to use our helper immediately on different controllers. Just call our function in any class
$companyData = companyData();
or in blade view
{{ companyData() }}
Let me know if you are facing an issue.

- 380
- 1
- 15
Below is how to share a variable with your entire application via the AppServiceProvider, You can also do this inside of your base controller in the construct method.
File: App\Providers\AppServiceProvider.php
<?php
namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::share('key', 'value');
}
}
You can then access $key
inside of any view.

- 3,339
- 2
- 14
- 35