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 :: laravel collective form include image 
Php :: how to enable pretty url in yii2 
Php :: specification migration laravel 
Php :: how to write tests for php 
Php :: laravel custom validation message 
Php :: db transaction laravel 
Php :: wc php if is product category page 
Php :: php check valid json string 
Php :: default language laravel 
Php :: what does defined di in php 
Php :: mobile detect in laravel 
Php :: laravel package for getID3() 
Php :: doctrine querybuilder print sql 
Php :: get recoed between two datetime laravel 
Php :: on running php file showing code instead of view 
Php :: select multiple option in laravel 
Php :: php undefined function mysqli_fetch_all() 
Php :: if function not exists php 
Php :: calculator in php 
Php :: wp add menu page and subpage 
Php :: laravel local scope 
Php :: unset php return array 
Php :: one lin if statement php 
Php :: php validate colour 
Php :: php add custom button in wordpress editor 
Php :: seprate day and year from laravel to timestamp 
Php :: error 500 internal server error in laravel 
Php :: php add new item to associative array 
Php :: how to setup php mailer 
Php :: laravel model uploaded file name 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =