Hello, According to Laravel documentation a sub view should have access to the same data as the parent, so I am trying to create a sub view and display some data by doing:
@include('pages.name');
This seems to work if I am only using static html. As soon as I try to access data from the main view, I get no error but a blank page.
In the main view I can do:
@foreach ($accounts as $account)
{{$account->typeDescription}}
@endforeach
and it works fine -- however it does not work in the subview.
If $accounts is a variable passed to the parent view, then it should be available to the included partial; you can explicitly pass data into a partial; e.g. if the variable name in the parent was different from the variable expected in a shared partial, e.g.
@foreach ($myAccounts as $myaccount)
{{$myaccount->typeDescription}}
@endforeach
But now I get a crash:
Facade\Ignition\Exceptions\ViewException|[email protected]|Undefined variable: myAccounts (View: C:\laragon\www\code\resources\views\home.blade.php)|C:\laragon\www\code\resources\views/home.blade.php
The variable in the included partial would be the key from the array (just as passing data to a view in a Controller action), so in the partial, you would have an $accounts variable, not $myAccounts. Anyway this was simply as example; you don't need to pass data from the parent (providing it is available in the parent)