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 :: codeigniter4 route optional parameter 
Php :: php get country code from country name 
Php :: woocommerce disable links on specific product 
Php :: php compare dates 
Php :: laravel composer create project 
Php :: symfony form submit on refresh 
Php :: check if input file is empty in php 
Php :: creating default object from empty value laravel 
Php :: php file_get_contents html with special characters 
Php :: if file is not selected in file input type php 
Php :: php split by 
Php :: defining constant in config laravel 
Php :: laravel edit form modal example 
Php :: error laravel 404 in server 
Php :: laravel CORS config `allowed_origins` should be an array 
Php :: in date function + 1 month and - 1 day in php 
Php :: laravel not run test 
Php :: oops concepts in php 
Php :: how to change validation message in laravel 
Php :: Add Text After or Before on the Shop Page/Archive Page 
Php :: laravel create custom route file 
Php :: encode zlib php 
Php :: AuthController 
Php :: php capture include 
Php :: php filter_var name 
Php :: rest api php 
Php :: php get referrer ajax 
Php :: php while select query 
Php :: 20 usd to php 
Php :: bd sms gateway, laravel sms gateway, sms sending library, bd sms, sms gateway 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =