【问题标题】:Retrieve relationship items from a collection从集合中检索关系项
【发布时间】:2017-07-04 17:28:02
【问题描述】:

在控制器中,我创建了一个集合:

$playershome = App\Team::where('id', $session->team1_id)->with('players')->first();

并调用我的观点:

        return view('member.home', compact('user', 'session', 'playershome', 'playersaway'));

在我的团队模型中,我:

    public function players(){
        return $this->belongsToMany(User::class, 'team_users');
}

集合已创建:

但我不知道如何在我的视图中检索“玩家”项目。 我尝试:

@foreach($playershome->players as $player)

但我得到了:

Invalid argument supplied for foreach()

怎么了?

更新视图:

                                            <div class="mt-body">
                                            <h3 class="mt-body-title"> {{$session->team1Id->name}} </h3>
                                            <p class="mt-body-description"> It is a long established fact that a reader will be distracted </p>
                                            <ul class="mt-body-stats">
                                                @foreach($playershome->players as $player)

                                                    <li class="font-green">
                                                        <img src="/storage/{{$player->avatar}}"></li>
                                                @endforeach
                                            </ul>
                                            <div class="mt-body-actions">
                                                <div class="btn-group btn-group btn-group-justified">
                                                    <a href="javascript:;" class="btn">
                                                        <i class="icon-bubbles"></i> Punti </a>
                                                    <a href="javascript:;" class="btn ">
                                                        <i class="icon-social-twitter"></i> K/D </a>
                                                </div>
                                            </div>
                                        </div>

【问题讨论】:

  • 你可以添加视图吗?
  • 问题中添加的视图
  • 更改 $playershome = App\Team::where('id', $session->team1_id)->with('players')->first();为此:$playershome = App\Team::where('id', $session->team1_id)->with('players')->get();
  • 刚试过,这是 get 的错误:Property [players] does not exist on this collection instance / 如果我在 foreach 中用 $playershome[0] 更改 $playershome,我得到同样的错误 Invalid为 foreach() 提供的参数。
  • 保留这个:$playershome =Team->players()::where('id', $session->team1_id)->with('players')->get();并将 foreach 编辑为: foreach($playershome as $player)

标签: laravel eloquent laravel-collection


【解决方案1】:

查看您在 cmets 中的聊天记录,我发现您在您的团队表中为名为 players 的属性使用相同的名称,并且与用户的关系称为 players。尝试将关系重命名为其他内容,在查询和 foreach 中更改它。

编辑:发现与您相同的问题的 stackoverflow 问题:Laravel get Eloquent relation by same name as its attribute

【讨论】:

  • 是的,问题是玩家列名和同名的关系方法冲突。
【解决方案2】:

试试这个方法

$playershome = App\Team::with('players')->where('id', $session->team1_id)->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2017-01-22
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多