【问题标题】:passing variables to laravel service provider将变量传递给 laravel 服务提供者
【发布时间】:2015-07-07 18:53:02
【问题描述】:

我想将我的 laravel 应用程序上的变量从视图传递给服务提供者。 观点是:

{!! 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() !!}

服务提供商处的代码是:

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 试过了,但还是没能成功。

我想知道如何将变量从视图传递给服务提供者,或者至少如何从服务提供者调用控制器方法。我试过了,但没能成功。感谢所有帮助。

编辑

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

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    假设您通过路由传递 $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} 参数。

    【讨论】:

    • 这会导致页面被重定向吗?因为这是一个局部视图
    • 您尝试做的是从客户端(即视图)向服务器(即 ServiceProvider )发出请求。如果在请求数据时不想离开视图,可以使用 Ajax 调用。那是另一回事。
    • 我想改变局部视图,而不是其他部分。我还没有在 laravel 上尝试过 ajax。但是该方法仍然给了我未定义的变量数据
    • 搜索使用局部视图的 Ajax。我认为那里会有答案。祝你好运!
    【解决方案2】:

    我不知道您从哪里获取 id,所以我假设它存储在请求对象中的某个位置。话虽如此,您可以在服务提供者的构造函数中对请求对象进行类型提示。然后通过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());
        });
    }
    

    【讨论】:

    • 这会抛出Argument 1 passed to App\Providers\DataServiceProvider::__construct() must be an instance of Request, instance of Illuminate\Foundation\Application given,
    • 哦,我明白了,父类期待收到Illuminate\Foundation\Application。我已经更新了答案,希望对你有用。
    • 它给了我另一个类似于第一个错误。 Argument 2 passed to App\Providers\DataServiceProvider::__construct() must be an instance of Request, none given
    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多