访问集合

Accessing Collections

我可以寻求帮助吗??

所以我与银行账户 + 可以访问它的用户有这种关系

return $this->belongsToMany('App\BankAccount', 'permissions', 'user_id', 
'account_id');

然后我把它传给我看

$view->with('access', User::where('id', $user->id)->with('bankAccounts')->get());

这是我访问它的方式

@foreach($access as $account)
     <li>{{ $account->bankAccounts }}</a></li>
@endforeach

它给了我这个输出

[{"id":1,"account_name":"MBTC Mambaling","type":"1","branch":"Mambaling","account_number":"331-7-331504366","contact_person":"Armand Dela Cruz","contact_number":null,"created_at":"2017-06-26 03:16:13","updated_at":"2017-06-26 03:16:13","pivot":{"user_id":2,"account_id":1,"created_at":"2017-06-26 03:16:39","updated_at":"2017-06-26 03:16:39"}}]

我如何访问集合中的每个数据?? 感谢大家提前回答

您正在return使用以下查询收集用户

User::where('id', $user->id)->with('bankAccounts')->get()

我建议将其更改为如下所示:

User::find($user->id)->bankAccounts()

这会将 return 用户附加 bankAccounts 作为一个集合。然后您可以像这样访问属性:

@foreach($access as $account)
    <li>{{ $account->account_name}}</a></li>
@endforeach