首页 > 解决方案 > 无法访问 laravel 对象属性

问题描述

我有一个问题,我似乎无法从 laravel 中的控制器获得返回的属性。

    $machineLogs = MachineLog::whereHas( 'groups', function( $group ) use (&$componentId) {
        $group->whereHas( "units", function( $unit ) use (&$componentId) {
            $unit->whereHas( "components", function( $component ) use (&$componentId) {
                $component->where( "Component", $componentId );
            } );
        } );
    } )->take( $limit )->get();


    foreach ($machineLogs as $log)
    {
        echo $log->oId;
    }

我似乎无法访问返回的对象的任何属性,但是我可以取回关联的数组,我做错了什么?

[
{
"oId": 411197,
"active": false,
}
]

对象本身具有我试图达到的不同属性,例如 $log->oId 以从日志中取回 oId。

我可以访问与 MachineLog 模型相关的组。{转储($log)的结果;}

App\MachineLog {#388 ▼
  #primaryKey: "Oid"
  #table: "Log"
  #connection: "testdb"
  #appends: array:1 [▼
    0 => "groups"
  ]
  #fillable: array:2 [▼
    0 => "name"
    1 => "oId"
  ]
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:20 [▼
    "Oid" => 10401419
    "Active" => true
    "Speed" => "0"
    "Weight" => "0"
    "Act" => "0"
    "Code" => ""
    "Control" => false
    "Type" => 1
    "Max" => 1
    "Name" => "Arl"
    "Number" => "9-10"
    "Mode" => 1
    "Recipe" => ""
    "Line" => "-1"

标签: phplaravel

解决方案


推荐阅读