Included data in laravel

In my laravel application, I pass the $ data variable to a view, which I will later include in another view. Therefore, in my controller method, I have

public function random($id){
    $data = DB::table('reports')->where('id',$id);
    return view('partials.data', compact('data'));
}

in partials.dataI have a:

{!! Form::open(['url'=>'reports/data',$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 on the main screen I have this function:

function kpi_values(d) {
    // `d` is the original data object for the row
    kpi = d.id;
    return '@include("reports.data", array("id" => "kpi"))';
}

which runs:

$('#monthly_table tbody').on('click', 'td.details-controls', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);
        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            row.child(kpi_values(row.data())).show();
            tr.addClass('shown');


        }
    });

when I run this, I get the following error:

ErrorException in 3534c4c98c65c2d5267bf7c54a960d41 line 13:
Undefined variable: data

I passed the variable data in my partial view, but it looks like it requires it in the main view. Is there a way to do this without passing the variable to the main view? I don’t want to mix things up because a partial view controller method requires a parameter, while there are no parameters in the main view.

All help is appreciated.

+4
source share
2

Laravel , , . . :

In \App\Providers\AppServiceProvider.php file

public function boot()
{

    //get data and pass it to partials.data whenever partials.data is executed

 view()->composer('partials.data',function($view){
   $view->with('data',DataSet::all());
 });  

}

, Laracast

+2

.

return view('partials.data')->share('data', $data);
0

All Articles