将变量传递给laravel服务提供商 [英] passing variables to laravel service provider

查看:60
本文介绍了将变量传递给laravel服务提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将laravel应用上的变量从视图传递给服务提供商. 该视图是:

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

错误:

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

我在Request上尝试过,但仍然无法使其正常工作.

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.

编辑

我从视图中的var变量获得$id

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

推荐答案

假定您使用$ c2类通过路由传递$ id,这在这种情况下非常有用.例如:

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

但是要小心,现在您的视图取决于{id}参数.

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

这篇关于将变量传递给laravel服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆