首页 > 解决方案 > laravel 非法偏移类型

问题描述

这是我在控制器中的代码

  $order = Order::with(['media','contact'])
        ->get();

这是我在视图中的代码,它工作正常

 <label> {{__($order['details'])}} </label>

但是如果我将属性更改为时间戳属性,例如 'created_at' 、 'updated_at' 我会收到此错误

Illegal offset type

请注意:视图中的代码在 foreach 内

标签: phplaravel

解决方案


问题是您将输出放入翻译函数中,而这些时间戳的输出是碳实例。

>>> __($u['created_at'])
PHP Warning:  Illegal offset type in /app/vendor/laravel/framework/src/Illuminate/Translation/Translator.php on line 111
PHP Warning:  Illegal offset type in isset or empty in /app/vendor/laravel/framework/src/Illuminate/Support/NamespacedItemResolver.php on line 25
PHP Warning:  Illegal offset type in /app/vendor/laravel/framework/src/Illuminate/Support/NamespacedItemResolver.php on line 43
PHP Notice:  Trying to access array offset on value of type null in /app/vendor/laravel/framework/src/Illuminate/Translation/Translator.php on line 330
PHP Notice:  Undefined offset: 1 in /app/vendor/laravel/framework/src/Illuminate/Translation/Translator.php on line 117
PHP Notice:  Undefined offset: 2 in /app/vendor/laravel/framework/src/Illuminate/Translation/Translator.php on line 117
=> Illuminate\Support\Carbon @1451606400 {#4467
     date: 2016-01-01 00:00:00.0 UTC (+00:00),
   }

但这里没有问题。

>>> $u['created_at']
=> Illuminate\Support\Carbon @1451606400 {#4465
     date: 2016-01-01 00:00:00.0 UTC (+00:00),
   }
>>> 

推荐阅读