String Resource in laravel programming?

1.4k Views Asked by At

is there any way that instead of hard coding strings in the blade you use something like a string resource(or string table) so if you change a string every blade will be changed . For example instead of hoard coding "product" in the blade I use a variable called "string1" so if one day I change string1 value to "service" every blade will be affected and fine.

4

There are 4 best solutions below

0
Alex Meyer On BEST ANSWER

maybe use localization with language files, so you can use

{{ trans('string1') }}

in a blade view. more info: https://laravel.com/docs/5.0/localization

and these 2 functions

trans
Translate a given language line. Alias of Lang::get.

$value = trans('validation.required'):


trans_choice
Translate a given language line with inflection. Alias of Lang::choice.

$value = trans_choice('foo.bar', $count);
0
Sitethief On

You could (mis)use a translation system like this one for this.

0
Sahith Vibudhi On

I am new to laravel but here is a idea I have.

create myString.php which contains your variables

example:

<?php
 $string1 = "products";
?>

in your files

<?php
 inlude_once("myString.php");
 {{ $string1 }}
?>

I hope this helped you to come up with a better solution.

good luck.

0
Laurel On

As of Laravel 5.4, you can use the double underscore:

{{ __('Register') }}

What is nice about this is that you don’t need to do anything else for it to output (in this case) “Register”.

You can change what value is displayed either in the appropriate messages.php file or by creating a json file, as described in the docs.