Accessing Laravel .Env Variables in Blade

Accessing Laravel .env variables in blade

Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications.
Here is full explanation:
https://www.youtube.com/watch?v=Q1ynDMC8UGg

php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear

How to call .env {{ env('APP_NAME} }} to laravel blade template?

In fact you should never use env helper directly in your application. All env should be put into config file and you should use config instead to avoid problems when config files are cached.

APP_NAME env is put into config file by default (see https://github.com/laravel/laravel/blob/master/config/app.php#L16 ) so in your Blade file you should use:

@section('project_title', config('app.name'))

Laravel - accessing .env variables

Route::get('/test', function () {
return "it is".config('app.name');
});

How to access the laravel .env variables inside javascript?

You can just echo out the env variable as a javascript string in your view:

<script>

var name = '{{ env('NAME') }}';

alert(name);

</script>

And your .env file would look like this:

NAME=Alex

Why Laravel Policy is not working in View/Blade?

You need to pass the actual instance of the question (and you don't need to pass the user class) if the policy is regarding a specific question:

@can(App\Policies\QuestionPolicy::UPDATE, $question)
<a class="text-sm font-light text-gray-600" href="#">Edit</a>
@endcan
@can(App\Policies\QuestionPolicy::DELETE, $question)
<a class="text-sm font-light text-gray-600" href="#">Delete</a>
@endcan

$user in the policy is always the currently signed in user.



Related Topics



Leave a reply



Submit