Search
 
SCRIPT & CODE EXAMPLE
 

PHP

define global variable in laravel blade

# Define Global Variables for Blade in Laravel 9
//1- Create a new service provider and call the share method within boot method
// app/Providers/ViewServiceProvider.php
<?php

namespace AppProviders;

use IlluminateSupportFacadesView;
use IlluminateSupportServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        View::share('variableName', 'value');
    }
}

//2- Register the service provider in the config/app.php file
<?php

return [
    // ...
    
    'providers' => [
        // ..

        AppProvidersViewServiceProvider::class,
    ],
];

//3- Clear configuration cache by using follwoing command
$ php artisan config:cache

//4- Now variable variableName can be used in all Blade templates
resources/views/users/index.blade.php

{{ variableName }}
Comment

laravel use global variable in model

SET configuration variables at runtime:
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        View::share('key', 'value');
        Schema::defaultStringLength(191);
        $company=DB::table('company')->where('id',1)->first();
        config(['yourconfig.company' => $company]);
    }
}

USE:
config('yourconfig.company');
Comment

laravel blade global variable

// AppServiceProvider

public function boot()
{
    View::share('globalVarName', 'Global Var Value');
}
Comment

how to declare global variable in laravel controller

class TestController extends Controller
{
    private $x;

    public function index()
    {
        $this->x ='22';
    }

    public function send_message()
    {
        echo $this->x;
    }
}
Comment

global variable in laravel controller

private $variable;
Comment

create global function laravel

 //Into the console
 php artisan clear-compiled
 php artisan config:cache
Comment

global variable in laravel

//create globals.php file in laravel/config/ and add the ff:

<?php
return [
    'user_type' => [
          'administrator' => 1,
          'hr'            => 2,
          'employee'      => 3
    ]
];

//then you can call it in your controllers or blade using
config('globals.user_type.employee')
Comment

create global function laravel

 //AppProvidersGlobalFunctionsServiceProvider.php

 public function register()
 {
     require_once base_path().'/app/Functions/GlobalFunctions.php';
 }
Comment

create global function laravel

Composer //Autload whitin composer.json method
|
|--->Laravel App //My method
     |
     |--->Controller //Trait method
     |--->Blade //Trait method
     |--->Listener //Trait method
     |--->...
Comment

create global function laravel

 //Into the console
 php artisan make:provider GlobalFunctionsServiceProvider
Comment

create global function laravel

 //AppConfigApp.php

 'providers' => [

     /*
     * Laravel Framework Service Providers...
     */
     IlluminateAuthAuthServiceProvider::class,
     ...
     IlluminateValidationValidationServiceProvider::class,
     IlluminateViewViewServiceProvider::class,
     AppProvidersGlobalFunctionsServiceProvider::class, //Add your service provider
Comment

create global function laravel

 //Use your function anywhere within your Laravel app
 first_function();
 second_function();
Comment

PREVIOUS NEXT
Code Example
Php :: laravel middleware 
Php :: php join 
Php :: $ is not define in laravel 
Php :: php localhost:8000 
Php :: asin() php 
Php :: drop foreign key laravel 
Php :: Calling itself a static function in php 
Php :: how to manually remove cache in laravel 
Php :: codeigniter4 route optional parameter 
Php :: php compare dates 
Php :: cron job every 5 minutes wordpress 
Php :: empty in php 
Php :: php file_get_contents html with special characters 
Php :: laravel create resource 
Php :: how to create cookie in laravel 
Php :: Laravel 8 - get values of url query strings in controller 
Php :: laravel collection methods 
Php :: get woocommerce my account page url 
Php :: laravel custom abort message 
Php :: How to Get Radio Button Value in PHP Without Submit 
Php :: get all taxonomy name wordpress 
Php :: is legged in wodpress 
Php :: A Livewire component was not found 
Php :: encode zlib php 
Php :: woocommerce unset custom checkout field 
Php :: is resource php 8 
Php :: Get PHP String Length 
Php :: entrust laravel 
Php :: php md5 password is insecure 
Php :: check if config exist laravel 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =