How can i include php file from blade in laravel

Hello Guys In Laravel Blade I Want To Include Php file i try to use @include but it doesn’t work because @include to include views not php file

My File I Want To Include

App\Helpers\Variables.php

$dufaultLang = get_dufault_lang();
$categories =
    mainCategory::where(
        'translation_lang',

        $dufaultLang
    )
        -> Selection()  // Select From Selection Scope
        -> get();       // Get Selection Data

MyBlade

<ul class="menu level1">

@include('App.Helpers.Variables.php') // Here It Doesn't Work

@if(isset($categories) && $categories -> count() > 0)

@foreach($categories as $category)
Hello
@endforeach

@endif

</ul>

I Can’t Include The $Categories From Variables.php I Try To Use @include but I see This Error

View [App.Helpers.Variables.php] not found.

>Solution :

use Sharing Data With All Views

In AppServiceProvider boot method

public function boot()
{
  $dufaultLang = get_dufault_lang();
  $categories =mainCategory::where('translation_lang', $dufaultLang)
                -> Selection()  
                -> get();       
  View::share('categories', $categories);
}

and in your view

<ul class="menu level1">
    @if(isset($categories) && $categories->count() > 0)

        @foreach($categories as $category)
            Hello
        @endforeach
    @endif

</ul>

Also, make sure follow to the naming convention for the model class

Leave a Reply