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
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.Assuming that you pass $id through a route, using
Router
class which is very useful in this case. For example :But be carefull, now your view is depend on {id} parameter.