在Laravel中访问配置变量

时间:2017-04-10 20:02:10

标签: php laravel config

在我的配置文件夹下,我有一个constants.php文件。我使用{{ json_encode(config('constants.pressMetadata')) }}访问了常量文件中的pressMetadata对象。这会将所有数据转储为JSON对象。我想要做的是使用foreach循环打印出所有数据。我试过了

@foreach (config('constants.pressMetadata') as $tile)
          <p>{{$tile->id}}</p>
@endforeach

这不起作用。那么我应该怎么做才能使用foreach循环来遍历c​​onfig中的对象('constants.pressMetadata')?

这是常量pressMetadata

'pressMetadata'=>[
      "AARP" => [
          "id" => 1,
          "company" => "AARP",
          "title" => "Updating Your Résumé for the Digital Age",
          "url" => "http://www.aarp.org/work/job-hunting/info-2016/give-resume-a-digital-reboot.html",
          "date" => "Sep 9, 2016"
      ],
      "Business Insider" => [
          "id" => 2,
          "company" => "Business Insider",
          "title" => "8 things you should always include on your résumé",
          "url" => "http://www.businessinsider.com/what-to-always-include-on-your-resume-2016-1",
          "date" => "Jan 28, 2016"
      ],
      "Morning Journal" => [
          "id" => 3,
          "company" => "Morning Journal",
          "title" => "5 things you missed: Google updates search, Jobscan and more",
          "url" => "http://www.morningjournal.com/article/MJ/20140124/NEWS/140129366",
          "date" => "Jan 24, 2014"
      ],
],

1 个答案:

答案 0 :(得分:4)

由于它是数组数组,请执行以下操作:

@foreach (config('constants.pressMetadata') as $tile)
    <p>{{ $tile['id'] }}</p>
@endforeach