20

From the documentation, only 4 parameters can be passed to @each. I don't think that using @include will help. Code is below

@each('partials.nav.categories', $groupCategories, 'parent')

I need to send through an additional variable for use in the partial partials.nav.categories. This variable is not contained within the $groupCategories array.

Is there any way to send this additional variable through or do I have to append it to each item in the partials.nav.categories array?

Thanks

8
  • You should be able to use the variables defined in that blade. Commented Jan 1, 2016 at 7:07
  • 1
    thats' the case, it cannot trace it. Simply undefined Commented Jan 2, 2016 at 17:18
  • Try passing all variables as an array. Ex: @each('partials.nav.categories', [$groupCategories,$moreVariable], 'parent') Commented Jan 4, 2016 at 10:14
  • where can I find the docs about it Commented Jan 5, 2016 at 7:40
  • 1
    As of Laravel 5.2 it's just not possible. Why don't you just use the @foreach and @include directives instead? They'll give all you the flexibility you need while still maintaining readability. Commented Mar 21, 2016 at 13:05

3 Answers 3

1

You can share variable from your controller view()->share('key', 'value'); That's value will be available across all of your views.

Or, you can create view composers exactly for this view.

public function yourmethod()
{
   view()->composer('partials.nav.categories', function($view) {
      $view->with('var', 'value');
   });

   return view('path.to.view', ['groupCategories' => $categories]);
}

And $var will be available only in partials.nav.categories view.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you misunderstand my question. The only problem is how do I pass additional variable trough @each blade.
@MuhaiminAbdul through each directive it's impossible
1

I think you are right. Append data to $groupCategories is the right way. As per documentation, The fourth param is what will be displayed if the $groupCategories is empty. You can either pass a view template, which will be shown only once, or any text prepended with raw| will be displayed as is.

General format:

@each('viewfile-to-render', $data, 'variablename','optional-empty-viewfile')

The first argument is the template to render. This will usually be a partial, like your nameofyourblade.blade.php.

The second one is the iterable dataset, in your case $groupCategories.

The Third is the variable name the elements will use when being iterated upon. For example, in foreach ($data as $element), this argument would be element (without the $).

The fourth argument is an optional one – it’s the name of the template file which should be rendered when the second argument ($data) is empty, i.e. has nothing to iterate over. If we apply all this to our case, we can replace this entire block:

@if (count($groupCategories) > 0)
    <ul>
    @foreach ($groupCategories as $parent)
        @include('partials.nav.categories', $parent)
    @endforeach
    </ul>
@else
    @include('partials.group-none')
@endif

with

@each('partials.nav.categories', $groupCategories, 'parent', 'partials.group-none')

3 Comments

ok thanks for enlightment. But I need extra data to be passed down to the iteration
If $groupCategories is a proper collection, you can use $groupCategories->isEmpty() instead of the count() check.
There is no access to the collection within the partial. Only the iterator.
0

You only need to pass to the partial instance, the variables that change on each use of the partial.

The code inside the partial itself can access anything that is in scope of the parent.

So, if for instance, the main view can use $user, so can the partial - on the understanding that it will be the same value on each inclusion of the partial.

Not sure if this helps since you don't say if the additional parameter needs to be different for each inclusion of the partial.

2 Comments

as you can see, I have $groupCategories variable looped in partials.nav.categories but I also have another variable called $mainCategory to used within the @each. It is used to check whether every item in the loop match or not match to the $mainCategory
@Snapey The question is related to @each, that doesn't propagates the variables as @include does. In a subview called using @each there is no access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.