“此集合实例上不存在属性[id]”,但是代码正确吗?

时间:2019-11-13 10:43:41

标签: php database laravel eloquent laravel-blade

我使用的是雄辩的ORM,它在用户(玩家)和房间(会话)之间存在多对多(n到n)的关系,其中用户与房间相连。他们能够打牌(打乱),以便通过他们的牌估计来评估软件问题。

当我加载使用可选{{id?}}参数的视图时,它会查看我的DB :: Class并通过该参数找到房间。

问题是,当我想访问附加用户的id属性以在我的视图中显示收到的信息时。尽管我的调试显示我收到了正确的数据,但是我得到了上面提到的错误。

我的观点: “ cards.blade.php”

<h1 align="center"><u>Userid</u> {{$currentUser->id}}</h1>
<h1 align="center"><u>Username:</u> {{$currentUser->name}}</h1>
<h1 align="center"><u>Your estimation is:</u> {{$currentUser->userValue}}</h1>
<h1 align="center">Last updated: {{$currentRoom->updated_at->diffForHumans() ?? ''}}</h1>
<h1 align="center">@if ($currentUser->isAdmin === 0 || null )
    'You are sadly not an admin'
    @else 'You are an admin'
    @endif</h1>
<h1 align="center">Room Number: {{$currentRoom->id}}</h1>

“ SessionController.php@getPlayerInfo”

  public function getPlayerInfo($id)

    {


        $currentRoom = Session::findOrFail($id);
        $currentUser = $currentRoom->users;

        $tmp = $currentUser->id;
        dd($currentUser);

        return view('sessions.cards')
            ->with('currentUser', $currentUser)
            ->with('currentRoom', $currentRoom);
    }

“ web.php”

Route::get('play/{id?}', 'SessionController@getPlayerInfo')
    ->name('sessions.cards')
    ->middleware('auth');
  

dd($ currentUser)输出:

Collection {#305 ▼
  #items: array:1 [▼
    0 => User {#302 ▼
      #fillable: array:3 [▶]
      #hidden: array:2 [▶]
      #casts: array:1 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "id" => 1
        "name" => "admin"
        "email" => "admin@email.com"
        "email_verified_at" => null
        "password" => "$2y$10$AhpAyD2NR2TW.kBYOMzTAe1kfRh73CtMhRxaGH0Bi2OrhPcSA65f."
        "userValue" => null
        "isAdmin" => null
        "remember_token" => null
        "created_at" => "2019-11-13 10:13:20"
        "updated_at" => "2019-11-13 10:13:20"
      ]
      #original: array:12 [▶]
      #changes: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
    }
  ]
}

2 个答案:

答案 0 :(得分:3)

这是一个集合。使用foreach之类的

@foreach($curretUser as $cuser)

<h1 align="center"><u>Userid</u> {{$cuser->id}}</h1>
<h1 align="center"><u>Username:</u> {{$cuser->name}}</h1>
<h1 align="center"><u>Your estimation is:</u> {{$cuser->userValue}}</h1>
<h1 align="center">Last updated: {{$currentRoom->updated_at->diffForHumans() ?? ''}}</h1>
<h1 align="center">@if ($cuser->isAdmin === 0 || null )
    'You are sadly not an admin'
    @else 'You are an admin'
    @endif</h1>
<h1 align="center">Room Number: {{$currentRoom->id}}</h1>
@endforeach

答案 1 :(得分:2)

正如dd所暗示的,您的$currentUser包含一个collection,因此它不是一条记录。您必须遍历其内容才能获得一个用户。像这样:

@foreach ($currentUser as $user)
    {{ $user->id }}
@endforeach