php - 在数据库中查询后,使用 PHP 在 Laravel 中正确获取数据

标签 php mysql laravel laravel-4

因此,我尝试根据用户的语言在 HTML 中选择表单中显示文本,我正在使用 Laravel,并且有一个简单的方法可以做到这一点:

Lang::get('texts.whatever');

这将转到 texts.php 文件,该文件将“whatever”更改为正确的文本。

此外,我有一个像这样的关联数组: $dropdown = ['1'=>'France','2'=>'Germany', '3'=>'Greece', '4' =>'葡萄牙', '5'=>'西类牙', '6'=>'英国'];

像这样:

@foreach($dropdown as $id=>$name)           
<?php
    $newArray[$id]=Lang::get('texts.'.$name);
?>
@endforeach

<?php
    $dropdown = $newArray;
?>

之后,我在选择表单时使用 $dropdown,它显示文本以及 $name 中的任何内容。它不会“获取”texts.php 中的文本。但是,如果我将 'texts.'.$name 更改为 'texts.yeah'(例如),它将正确填充选择。

我尝试了eval()exec()等,但我不知道还能做什么。也许我没有正确使用它们。

有什么建议或建议吗?预先感谢您。

最佳答案

您的 texts.php 文件看起来如何?

我做了一个小测试用例,它对我来说效果很好

lang/en/countries.php

return array(
    'France' => 'France in english',
    'Germany'     => 'Germany in english',
    'Spain'     => 'Spain in english',
);

routes.php

Route::get('countries', function()
{
    $countries = [1 => 'France', 2 =>'Germany', 3 => 'Spain'];
    $dropdown = [];

    foreach ($countries as $key => $value) {
        $dropdown[$key] = Lang::get('countries.'.$value);
    }
    dd($dropdown);
});

返回:

array (size=3)
  1 => string 'France in english' (length=17)
  2 => string 'Germany in english' (length=18)
  3 => string 'Spain in english' (length=16)

更简单的routes.php示例

Route::get('countries', function()
{
    $countries = Lang::get('countries');
    $dropdown = [1 => $countries['France'], 2 => $countries['Germany'], 3 => $countries['Spain']];
    dd($dropdown);
});

返回:

array (size=3)
  1 => string 'France in english' (length=17)
  2 => string 'Germany in english' (length=18)
  3 => string 'Spain in english' (length=16)

关于php - 在数据库中查询后,使用 PHP 在 Laravel 中正确获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012935/

相关文章:

php - 将 PHP Mysql 日期格式输出为 ISO 8601,仅带 "T"符号

php - Windows Azure 上的 PHP 社区在哪里?

mysql - 如果不存在则插入表,否则更新

PHP mysql_fetch_assoc() 错误

php - 如果 'date` 是 'today' ,触发从数据库中删除行的最佳方式?

php - 如何通过读取cookie来隐藏div服务器端

php - 设置嵌套表时出现问题。来自 cakephp Controller 的数据

Laravel 4 错误 : Undefined method shutdown

mysql - Laravel 的 SQL 连接问题

php - vagrant up 错误,改变所有权/vagrant : not a directory