1

I am new in Laravel, and currently coding an intranet app, which is basically a dashboard with a lot of informations, with Laravel 5.2, and one of the requirements is to be able to navigate thu different stores. Each page of the app, will require the CODE of store.

Currently I have a column store_id in all my tables, and I use GET with routes to retrieve this value, like:

www.mysite.com/1/employees -> will list all employees from Store 1
www.mysite.com/1/employees/create -> will create employee to Store 1
www.mysite.com/2/financial -> will list all financial widgets with data from Store 2

I would like to remove my STORE_ID from the GET, and use a DROPDOWN select with all stores in my topbar.blade.php, for example:

<select>
  <option selected>Store1</option>
  <option>Store2</option>
</select>

Whenever someone select "Store1" or "Store2", I would like to get the Store information, using StoreController and make this variables available to all controllers and views. Where I can use the following URL

www.mysite.com/employees -> will list all employees from "Depending of the SELECT"
www.mysite.com/employees/create -> will create employee to "Depending of the SELECT"
www.mysite.com/financial -> will list all financial widgets with data from "Depending of the SELECT"

I have read about View Composer, Facades, ServiceProvide, and I got really confused about all of them.

Fábio Galera
  • 125
  • 11

3 Answers3

2

You can also share common data from a provider eg. AppServiceProvider or a provider of your own. I am using AppServiceProvider here for example. In AppServiceProvider boot method:

public function boot()
{
    $this->passCommonDataToEverywhere();
}

Now write in the method:

protected function passCommonDataToEverywhere()
{
    // Share settings
    $this->app->singleton('settings', function() {
        return Setting::first();
    });
    view()->share('settings', app('settings'));

    // Share languages
    $this->app->singleton('languages', function() {
        return Language::orderBy('weight', 'asc')->get();
    });
    view()->share('languages', app('languages'));
}

In this example I have to use:

use App\Language;
use App\Setting;
im_tsm
  • 1,965
  • 17
  • 29
  • Hi, Thanks for you answer. Whenever I select a store in the top.blade.php, how do I call this ServideProvider with a variable ? public function boot() { $this->ShareStore(); } protected function passCommonDataToEverywhere($id) { // Share settings $this->app->singleton('store', function() { return Store::where('id',$id); }); view()->share('store', app('store')); } – Fábio Galera Jul 14 '16 at 11:05
  • `public function boot (Router $router) { $router->bind('id',function($id) { $this->ShareStore($id); }); }` But I think you should use it carefully, as it is now dependant on {$id} param – im_tsm Jul 15 '16 at 07:38
1

It's not that hard really. There are may be other methods but I prefer to do it this way:

Sharing Data:

Open app/Http/Controllers/Controller.php and add aconstructor function like this:

<?php

namespace App\Http\Controllers;

...

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        $this->sharedVar = "I am shared.."; // to share across controllers
        view()->share('sharedVar',$this->sharedVar); // to share across views
    }
}

Using Data:

1. In Controllers:

All controllers extend the above controller. So the property is available to all controllers:

class YourController extends Controller
{
    public function index()
    {
        dd($this->sharedVar);
    }
...
}

2. In Views:

{{$sharedVar}} // your-view.blade.php

EDIT:

If you want to share data to places other than controllers and views, the best method perhaps is to use AppServiceProvider:

Open app/Providers/AppServiceProvider.php and update the boot() method:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->singleton('sharedVariable', function () {
            return "I am shared";
        });
    }

    ...

}

Usage:

dd(app('sharedVariable')); // anywhere in the application
Naveed
  • 879
  • 1
  • 9
  • 20
  • I liked this one, but the only "problem" (at least for me hehe) is to use the plugin Widgets, which extends an AbstractWidget. Would it be available there as well ? – Fábio Galera Jul 14 '16 at 11:00
  • Great, thanks so much. So it lead me to another question. For example, whenever someone select Store 1, I will call as POST/GET the StoreController to retrieve all data from that Store, and how do I share this information dinamically ? How I can pass a object as parameter to the ServiceProvider ? – Fábio Galera Jul 14 '16 at 12:03
  • Can I do something like this in StoreController ? app('sharedVariable') = 'Hello'; – Fábio Galera Jul 14 '16 at 12:13
0

Iam wondering, if the following would be possible:

-- StoreController

public function BindStore($id)
{
    $store = Store::where('id', $id);
    App::singleton('store', $store);
}

Or maybe using Services

Fábio Galera
  • 125
  • 11