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

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

PREVIOUS NEXT
Code Example
Php :: last item coma replace and php 
Php :: laravel trim string blade 
Php :: eager load relationships by default in the model laravel 
Php :: sendmail php 
Php :: laravel number input positive only 
Php :: get specific word from string php 
Php :: laravel blade @auth 
Php :: php static variable 
Php :: laravel get view variables 
Php :: - in php 
Php :: php define array first 10 number 
Php :: laravel chunk 
Php :: Show all laravel valet folders 
Php :: exception in php or try catch in php 
Php :: string between two strings 
Php :: get date to current week last or first day dates 
Php :: php declare array 
Php :: drupal get node id from twig 
Php :: php header not working 
Php :: php find multiple value in array 
Php :: php pdo error 500 
Php :: wordpress enqueue if shortcode 
Php :: update cart subtotal woocommerce 
Php :: wordpress - php settings 
Php :: how to redirect to another page in php automatic after 2 second 
Php :: mac install php-fpm 
Php :: laravel array in lang 
Php :: wc get product category image 
Php :: laravel url with parameters blade 
Php :: php get multiple url parameters 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =