Home > Mobile >  How can i include php file from blade in laravel
How can i include php file from blade in laravel

Time:08-13

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 >

@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.

CodePudding user response:

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 >
    @if(isset($categories) && $categories->count() > 0)

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

</ul>

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

As suggested by @Snapey in the comment, to avoid executing a query every time, you can use Cache to store the query result

  • Related