Php 将变量传递给laravel服务提供商
我想将我的laravel应用程序上的变量从视图传递给服务提供商。 意见是:Php 将变量传递给laravel服务提供商,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想将我的laravel应用程序上的变量从视图传递给服务提供商。 意见是: {!! Form::open(['url'=>'reports/data',$kpi_id]) !!} <table class="table table-responsive table-condensed table-bordered tab-content"> <thead> <tr> <
{!! 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() !!}
错误:
Argument 2 passed to App\Providers\DataServiceProvider::App\Providers\{closure}() must be an instance of App\Http\Requests\Request, none given
我按要求试了一下,但还是没能成功
我想知道如何将变量从视图传递到服务提供者,或者至少如何从服务提供者调用控制器方法。我试过了,但没能成功。感谢所有的帮助
编辑
我从视图中的
var
变量中获取$id
,我不知道从何处获取id,因此我假设它存储在请求对象中的某个位置。也就是说,您可以在服务提供者的构造函数中键入hint请求对象。然后通过use
关键字将id传递到回调函数中
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());
});
}
假设您通过路由传递$id,使用
Router
类,这在本例中非常有用。例如:
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);
});
}
但是请小心,现在您的视图依赖于{id}参数。这将抛出传递给App\Providers\DataServiceProvider::\uu construct()必须是Request的实例,light\Foundation\Application的实例,哦,我明白了,父类希望收到
light\Foundation\Application
。我已经更新了答案,希望对你有用。它给了我另一个类似于第一个的错误<代码>传递给App\Providers\DataServiceProvider::\uu construct()的参数2必须是请求的实例,没有给定的实例这是否会导致页面重定向?因为这是一个局部视图,所以您尝试做的是从客户端(即视图)向服务器(即服务提供者)发出请求。如果在请求数据时不想离开视图,可以使用Ajax调用。那是另一回事。我希望局部视图改变,但另一部分不改变。我还没有在拉威尔身上尝试过ajax。然而,该方法仍然为我提供了使用部分视图的Ajax的未定义变量数据搜索。我想会有答案的。祝你好运
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);
});
}