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

问题描述 投票:1回答:2

我想向用户显示信息,显示他们在视图内部的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_listcommunity_iduser_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

提前致谢 ;)

php laravel
2个回答
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()和徽章列表,它本身就是集合的集合。


2
投票

试试这个解决方案它只有2个查询

public function index()
{
    $communitiesIds = CommunityMemberList::where('user_id', Auth::user()->id)->pluck('community_id');
    $communities = Community::whereIn('id', $communitiesIds)->get();

    return view('home', ['communities' => $communities, 'badges' => $badges]);
}
© www.soinside.com 2019 - 2024. All rights reserved.