【问题标题】:Laravel: Trying to access value inside array in view templateLaravel:尝试在视图模板中访问数组内的值
【发布时间】:2018-12-19 18:19:26
【问题描述】:

所以我试图在我创建的数组中访问几对 key->values 的值并将其传递给名为 config 的视图,我想像这样访问它们:{{ $config->web_name['value'] }} 或 @987654323 @ 但两者都返回错误。

这就是我将数据传递给视图的方式:

public function inicio()
    {

        $pageInfo = 
        [
            'page_title'      => 'Inicio',
            'menu_active'     => 'Inicio',
            'submenu_active' => '',
        ];

        $globals = Globals::all();

        $web_name = $globals->where('name', 'Nombre del restaurante')->first();
        $web_description = $globals->where('name', 'Descripcion en google del restaurante')->first();
        $restaurant_bio = $globals->where('name', 'Biografia del restaurante')->first();
        $booking_phone = $globals->where('name', 'Télefono de reserva')->first();
        $client_mail = $globals->where('name', 'Email de contacto')->first();
        $full_address = $globals->where('name', 'Dirección del restaurante')->first();
        $city_zip_address = $globals->where('name', 'Código postal y ciudad')->first();

        $config = compact('web_name', 'web_description');

        $sliders = Slider::all();


        return view('inicio.index', compact('pageInfo', 'config', 'sliders', 'web_name'));
    }

【问题讨论】:

  • 粘贴错误详情
  • 试图获取非对象的属性“web_name”(查看:C:\xampp\htdocs\Restaurante1\resources\views\inicio\index.blade.php)
  • 你的 $config 不是对象。是数组。稍微阅读一下错误,它解释得很好。

标签: javascript php arrays laravel


【解决方案1】:

您正在尝试使用像对象一样传递给视图的变量,但是当您 do compact() 时,您正在使它们成为 arrays。因此,要访问您的数据,请执行以下操作:

{{ $config['web_name']['value'] }}

【讨论】:

    【解决方案2】:

    试试下面。此外,您正在压缩 $web_name 两次。

    {{$web_name['value']}}
    

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 1970-01-01
      • 2021-10-07
      • 2021-01-09
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      相关资源
      最近更新 更多