Tuesday, May 15, 2018

Laravel: passing dynamic variable thru sub-views - development

Laravel: passing dynamic variable thru sub-views

I’m using Laravel 5.1 and need to pass dynamic counter to sub-views from main view. Each sub-view should use the counter value from previous sub-view. But the problem is that each sub-view initialize the counter from the beginning.
I’m using view composer to initiate the counter:

class PDFComposer
{

    public function compose(View $view)
    {
        $view->with('pdfCount', 0);
    }
}

Then link it to the view in my Service Provider:

public function boot()
{
    view()->composer('summary', PDFComposer::class);
}

Then I have summary view with 4 answer sub-views:

@include('answer', ['answers' => $group0])
@include('answer', ['answers' => $group1])
@include('answer', ['answers' => $group2])
@include('answer', ['answers' => $group3])

Each sub-view updates the counter by one based on some criteria. I want next ‘answer’ sub-view to use the last counter value from previous ‘answer’ sub-view. But each view uses ‘0’ as initial value.



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-passing-dynamic-variable-thru-sub-views/
via Lzo Media

No comments:

Post a Comment