Laravel - modify and return variable from @include

I am including multiple templates in a loop using

@include('template.included')

      

Before including this template, I define

$page = 0;

      

and inside the template, several times it calls

$page++;

      

Inside the included template, the value is incremented correctly, but outside the template, it seems like the $ page variable remains the same - it's like @include, creates its own copy of every var you use.

I need this $ page variable to update inside these templates, and its value goes back to my main template - am I missing something simple?

Appreciate any suggestions!

EDIT:

My problem for example:

$page = 0;
@include('template.included') //This calls $page++ 5 times
echo $page; //returns 0

      

I need $ page to return 4, not 0

+3


source to share


1 answer


Your guess is correct, Laravel's view system creates a new area for each view loaded. The method Illuminate\View\Factory::make()

that is used when enabling views does the following:

new View($this, $this->getEngineFromPath($path), $view, $path, $data)

      



It creates a new view and passes data to it, which effectively means that it sends a copy of the information. Since there are no arguments passed by reference (which is deprecated and has been removed in newer versions of PHP), what you want cannot be accomplished.

Speaking of which, your approach seems flawed. It seems to me that you are building the pagination in the view, which you shouldn't be doing. Laravel already offers a very easy way to create a Pagination , you should perhaps look into this.

0


source







All Articles