Laravel include without inheriting variables from parent

Viewed 119

I want to

@include('something', [])

without variables from parent. Is that possible?

Even though the included view will inherit all data available in the parent view, you may also pass an array of additional data that should be made available to the included view

from https://laravel.com/docs/8.x/blade#including-subviews

The problem with components is that you have to do sort of a split. E.g. say you have a page in

resources/views/mything/guest/buyer/layout/mysubview.blade.php

now I want to mark that something is related to this view, by creating an include folder say

resources/views/mything/guest/buyer/layout/includes/myspecialtableheader.blade.php

if I put it into a component, that is really far away from the folder where it is being used.

is there a different solution to having includes not having access to variables? or maybe something entirely different. Components which I can put wherever maybe. Maybe one can do something with @component ?

1 Answers

No, at least impossible at time of writting (2022).

Blade does not even provide a check, like:

@if(View::hasOwn('myVariable'))
    Do something ...
@endif

Which would distinguish passed variables from existing ones (but it's sudo-code, and hasOwn-method does not exist).

Also, the only workaround I can think of is prefixing, like:

@include('layouts.myLayoutAlpha', [
   'myLayoutAlpha_myVariable' => 'some value',
])
Related