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 :: get data in php 
Php :: laravel @env 
Php :: laravel database backup 
Php :: php class extends two classes 
Php :: @can in laravel 
Php :: if else php 
Php :: input if not null laravel 
Php :: Laravel how to use @auth and @guest with multi authentication 
Php :: share to facebook from website laravel 
Php :: laravel find 
Php :: create xml php 
Php :: apache 2 
Php :: Array (key and value) 
Php :: send data to api php 
Php :: inner pages not found yii 
Php :: Bd phone number validation in laravel 
Php :: php system info script 
Php :: Users/admin/Library/Caches/composer/files/laravel/laravel/1548f0533da115f0828fab4ef0c3923cd57879b6.zip): Failed to open stream: Permission denied 
Php :: replace special characters from string in codeigniter 
Php :: Problem getting updated value from child component to the parent component in a Laravel 9 with Vue 
Php :: how to register a file in a config in laravel 
Php :: wordpress get post type from url 
Php :: laravel count soft delete data 
Php :: php tools for visual studio package did not load correctly 
Php :: how to remove payment link in invoice woocommerce 
Php :: sorting table row data with php 
Php :: Personnaliser le logo de connexion WordPress sans plugin 
Php :: Formatting an Excel Column 
Php :: web.php file handling user request 
Php :: how to cooncet dabase eith laraavel 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =