如何显示数组中对象中的数组数据?

时间:2019-03-10 14:35:36

标签: php laravel

我想向用户显示信息,以显示他们在视图内部communities所在的位置。这是我要获取数据的控制器方法:

public function index()
{
    $communities = array();

    $usersCommunities = CommunityMemberList::where('user_id', Auth::user()->id)->get();

    foreach ($usersCommunities as $community) {
        $communitiesInfo = Community::where('id', $community->community_id)->get();

        array_push($communities, $communitiesInfo);
    }

    return view('home', compact('communities', 'badges'));
}

$userCommunities从表中获取用户拥有的所有社区ids。然后,我想使用community_id从另一个表中获取所有社区信息。

所以基本上有2个表:

  • community_member_list,其中有一个community_id和一个user_id
  • communities,其中包含与社区无关的信息

第一个表中的community_id将对应第二个表中的id

此方法有效,它将获取用户所在的所有社区信息数据,并从$communities循环将其推入foreach数组后返回此数据:

array:2 [▼
  0 => Collection {#236 ▼
    #items: array:1 [▼
      0 => Community {#249 ▼
        #connection: "mysql"
        #table: "communities"
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:10 [▶]
        #original: array:10 [▶]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▶]
      }
    ]
  }
  1 => Collection {#250 ▼
    #items: array:1 [▼
      0 => Community {#251 ▼
        #connection: "mysql"
        #table: "communities"
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:10 [▶]
        #original: array:10 [▶]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▶]
      }
    ]
  }
]

但是,如果我将其返回到视图,则无法访问特定数据值,例如社区namedescription。而且,如果我尝试使用类似array_poparray_shift之类的东西,它只会返回最后一个社区数组或第一个社区数组。

(这是供参考的视图文件:)

@if(!empty($communities))
    @foreach($communities as $community)
        <div class="card communityCard bg-dark col-md-3">
            <img class="communityIcon" src="/images/communityIcons/{{$community->icon_path}}">

            <div class="communityInformation">
                <span class="communityTitle">{{$community->name}}</span>
                <span class="userCount"><i class="fas fa-users"></i> {{$community->user_count}}</span>
            </div>

            <button class="btn launchCadBtn btn-primary  btn-sm">Launch CAD</button>
      </div>
    @endforeach
@else
    <span class="subTitle">You are not in any Communities. To join a Community, press the <i class="fas fa-plus-circle"></i> button and enter a Community ID</span>
@endif

预先感谢;)

2 个答案:

答案 0 :(得分:2)

只需在您的控制器中返回以下值:

public function index()
{
    $communities = array();

    $usersCommunities = CommunityMemberList::where('user_id', Auth::user()->id)->get();

    foreach ($usersCommunities as $community) {
        $communitiesInfo = Community::where('id', $community->community_id)->first();

        array_push($communities, $communitiesInfo);
    }

    return view('home', ['communities' => $communities, 'badges' => $badges]);
}

该视图应该可以正常工作。问题是您尝试将$communities作为集合来访问,但实际上是一个社区数组,由于 get()和徽章列表的原因,它本身就是一个集合的集合。

答案 1 :(得分:2)

尝试此解决方案,它只有2个查询

t01: No such file or directory