passing variables to laravel service provider

2019-08-07 17:55发布

问题:

I want to pass a variable on my laravel app from the view to the service provider. The view is:

{!! Form::open(['url'=>'reports/data',$kpi_id]) !!}

    <table class="table table-responsive table-condensed table-bordered tab-content">
        <thead>
            <tr>
                <th>Month</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
            @foreach($data as $dat)
                <tr>{{$dat->month}}</tr>
                <tr>{{$dat->value}}</tr>
            @endforeach
        </tbody>
    </table>

{!! Form::close() !!}

and the code at the service provider is:

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

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
}

public function composeData()
{
    view()->composer('reports.data', function ($view, $id) {
        $view->with('data', DB::table('reports')->where('kpi_id', $id)->orderBy('month', 'desc')->take(5)->get());
    });
}

The error:

Argument 2 passed to App\Providers\DataServiceProvider::App\Providers\{closure}() must be an instance of App\Http\Requests\Request, none given

I tried it with Request but still didn't manage to make it work.

I want to know how to pass the variable from the view to the service provider, or at least how to call the controller method from the service provider. I tried it but failed to make it work. All the help is appreciated.

EDIT

I get the $id from a var variable in the view

回答1:

Assuming that you pass $id through a route, using Router class which is very useful in this case. For example :

use Illuminate\Routing\Router; // include in your ServiceProvider

public function boot(Router $router)
{
    $router->bind('id',function($id){ // route:  /reports/{id}
        $this->composeData($id);
    });
}

public function composeData($id)
{
  $result = DB::table('reports')->where('kpi_id', $id)->orderBy('month', 'desc')->take(5)->get()

   view()->composer('reports.data', function ($view) use ($result) {
    $view->with('data', $result);
});
}

But be carefull, now your view is depend on {id} parameter.



回答2:

I don't know where you are getting id from so I'm assuming it's stored somewhere in the request object. With that said, you can type-hint the request object in the service provider's constructor. Then pass the id into the callback function via the use keyword.

public function __construct($app, \Request $request)
{
    parent::__construct($app);

    $this->request = $request;
}

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

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
}

public function composeData()
{
    $id = $this->request->get('your_id');
    view()->composer('reports.data', function ($view) use($id) {
        $view->with('data', \DB::table('reports')->where('kpi_id', $id)->orderBy('month', 'desc')->take(5)->get());
    });
}