Passing Company Data to All controller in Laravel 6.x

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 to use in controllers or blades files.

Let’s create a helper!

  1. 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
}
  1. 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
  1. 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.

Views - Laravel, Creating Views; Passing Data To Views. Sharing Data With All Views served by your application and separate your controller / application logic from your  company blog. By using our site, Passing data from blade to controller Laravel. Ask Question Asked 1 year, 7 months ago. Active 1 year, 6 months ago.

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.

Controllers - Laravel, Instead of defining all of your request handling logic as Closures in route files, you You may register many resource controllers at once by passing an array to   Today, We want to share with you Laravel Passing Multiple Variables from Controller to View.In this post we will show you Passing multiple arrays to a view, hear for Passing Variables from the Controller to View we will give you demo and example for implement.In this post, we will learn about php – Laravel – Pass more than one variable to view with an example.

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);
            }
        });
    }
}

Sharing Data Between Views Using Laravel View Composers , Passing data from a controller to a view is as simple as declaring a variable and adding it as a parameter to the  All routes and controllers should return a response to be sent back to the user's browser. Laravel provides several different ways to return responses. The most basic response is returning a string from a route or controller. The framework will automatically convert the string into a full HTTP response:

How to Build a Laravel 6 CRUD App with Auth0 Authentication, Laravel 6 crud travel app demo user account/dashboard screenshot hotel list page needs to list all the hotels saved in the database, so it needs a controller Therefore, we need to pass in the id of the reservation so that we can grab it and display it Identity & Security · Business · Culture · Engineering · Announcements  Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application. Using the make:controller Artisan command, we can quickly create such a controller:

Creating Your First Laravel App and Adding Authentication, Check out Build a Laravel 6 CRUD App with Authentication to learn what new it is the most starred PHP project on Github and a lot of companies and people all Controllers handle user requests, retrieve data from the models, and pass  Passing data to views. In this topic, we will learn that how we can pass the data to views. There are various ways of passing data to views: By using the name array; By using with() function; By using compact() function; Name array. The name array is the array of data that is passed as the second parameter to the view() method. Let's understand

How to Send Data Between Controller, Model and View in Laravel , We can pass data directly from routes or through the controller. In the code below we have stored all the values in a variable and passed it in the second  Laravel provides several different approaches to validate your application's incoming data. By default, Laravel's base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.

Comments
  • You can make the helper function for it. that function you can access in blade file, controller as well route file.
  • Your question is a bit confusing, are you asking how do you share it to all controllers or all views?
  • @MichaelMano I am trying to share the company details (it have folder name) to all controllers and all views as well. thanks!!
  • 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..
  • this should work, you just need to check if the user is logged in and then do a query
  • Thanks. It worked. But how to pass it into all controller as well..
  • 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
  • or you can use view()->shared('companyData') to get the shared value