Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

many tables, each needs to display different columns

I have a page that has a list of links to data tables. Each table shows different dataset with different column names. So the columns and column headers are different for each table. I am trying to make Laravel display all these tables using the same Blade file.

I was thinking about using Localization: https://laravel.com/docs/9.x/localization

For example, for the next table:

id | first_name | last_name | date_of_birth

I will do something like:

// lang/en_US.json
{
    "first_name": "first name",
    "last_name": "last name",
    "date_of_birth": "date of birth",
}

Then replace the column names in Blade: (fetching the necessary columns from the Controller)

// for each column . .
<th> {{ __($column_name) }} </th>

// and so on

In this example it may look odd because I used English, but the actual app needs more languages other than English.

Is this a good solution or I should use something else?

*The only thing I'm not sure about is that this is not the actual purpose of localization - it's supposed to translate from one language to another, not from some values to english (and then to other languages)

0 likes
13 replies
vincent15000's avatar

I don't understand if your question is about the internationalization or about the way to display data tables without knowing which table is shown.

1 like
Ligonsker's avatar

@vincent15000 It's both because the columns names from the db are unknown and also I need to translate them. I also edited the post to make it more readable, I read it badly at first (I think it's better now)

1 like
vincent15000's avatar
Level 63

@Ligonsker These are two different problems.

If you need to translate the columns names, you can use either a JSON file or a PHP language file as mentioned in the Laravel documentation.

For your second question about the columns names, you can retrieve the columns names using this method, for example for the Post model.

$postColumns = Schema::getColumnListing('posts');
1 like
Ligonsker's avatar

@vincent15000 and then mutate the columns names in the controller itself?

So not translate stuff like first_name etc in the localization files but rather in the controller, then use localization to translate these columns wherever they are displayed (the blade table in this case)

1 like
vincent15000's avatar

@Ligonsker Why not in the localization files ? The purpose of a controller is not to manage the translations. But in the controller you can retrieve the columns names, then you can display them in the view.

For example.

// if $postColumns[0] == 'title';
// if $postColumns[1] == 'page_number';

foreach ($postColumns as $postColumn) {
	{{ __($postColumn) }} // here 'title' and 'page_number' will be translated
}
1 like
Ligonsker's avatar

@vincent15000 But I also need to translate them from the column names (underscore for example) to English first

Because if the name of the column in the db is page_number then before I translate it from English to another language I'll also need to convert it to "page number" without the underscore in this example.

So first it would be a translation from db column name to English, then localization files would translate from English to another language?

Or I will also use localization to translate db columns to English

2 likes
vincent15000's avatar

@Ligonsker Oh you would like to translate page_number to Page number for example ?

You can for example try to use these functions.

use Illuminate\Support\Str;
...
$englishName = Str::ucfirst(Str::replace('_', ' ', $postColumn)); // will transform page_number to Page number
1 like
Ligonsker's avatar

@vincent15000 I know but this was a simple example, sometimes the column names just don't match exactly and not always consistent (This was the underscore example which replace would work, but in my case there are many columns that just don't match 1:1 to the actual translation)

1 like
vincent15000's avatar

@Ligonsker There is sadly no real solution to solve your problem. Laravel and PHP are not magic, if you need to display some translated names, you necessarily need to write all these translations in a file.

1 like
Ligonsker's avatar

@vincent15000 exactly! so I should first have a separate file (not localization) that is in charge for translating DB columns to English, and then use Laravel localization to translate that?

1 like
vincent15000's avatar

@Ligonsker That's exactly what I mean.

Why don't you do that ? But the code won't be able to guess the names.

1 like
Ligonsker's avatar

@vincent15000 thanks, will try. What do you mean by "But the code won't be able to guess the names" ?

1 like

Please or to participate in this conversation.