0

I'm building my first project in laravel and I have an index page where I want to show 4 different lists (they're different categories of the same data, and users will have permissions to see one or more of the lists)

The format for the lists (name, link, image, etc) is identical so I made a partial for it, with the idea that on this index page I'd check the authenticated user and if they have permission for list A then show it, if they have permission for list B then show it, etc.

To try it out, I made a scope for the first category (in my controller), and it shows the right data when I pass it to a totally separate view. But I'm unclear on how/whether I can display the partial multiple times on the index view with a different scope each time. What's the right way to go about this?

I imagined I would do something like check the permissions one at a time, make a variable with just that scope of data, and show the partial for just that data, like:

@include ('partials._items_list') , $category_A)

But the comment here suggests that a view has only one scope of data at a time (so maybe my idea isn't a valid thing): Laravel - Modify and return variable from @include

Community
  • 1
  • 1
Diane Kaplan
  • 1,626
  • 3
  • 24
  • 34

1 Answers1

1

You can pass different values with the same partial using something like the following:

@include ('partials._items_list') , ['categoey_A' => $category_A]);

@include ('partials._items_list') , ['categoey_A' => $category_B]); // <-- Different Value

Use only {{ $categoey_A }} in your partials._items_list view but output will be different.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Oh wow this is great news if this'll work! To make sure I understand, if I rephrase your suggestion with these names: ` @include ('partials._people_list'), ['scope' => $category_A]); ` Do I then need to refer to that 'scope' in the partial itself? Somewhere else? Or is there a name for this concept that I can read up on somewhere? thank you!! – Diane Kaplan Oct 11 '15 at 12:38
  • In your partial view you can use `{{$categoey_A}}` but both will contain different results. – The Alpha Oct 11 '15 at 18:13
  • 1
    That's it! You got it!!!! So yes- now I can specify the set of data that the partial will load- this is awesome :):) The only missing step now (simpler, I think), is how to actually instantiate the variable on the index view that'll call the partial, which I actually have as a separate question: http://stackoverflow.com/questions/33065270/laravel-newbie-syntax-for-grabbing-data-from-a-function-in-the-controller – Diane Kaplan Oct 12 '15 at 01:16