尝试从API获取json中非对象的属性

I'm using the Discord API to get the members on a Discord guild to display 10 randoms users and display it in the view with blade using the foreach method.

我从API获取JSON,获取它的members数组并对其进行裁剪以仅返回10个成员,以将其从控制器发送到视图。

The problem is that, when I want to parse it and use an element of the array (avatar_url), it's not working and I'm getting this error message: Trying to get property 'avatar_url' of non-object

这是我的控制器

class IndexController extends Controller {

    ...

    /**
     * Function to get 10 random players on fivem
     * @return array
     */
    private function getServerPlayers() {
        $request = file_get_contents('https://discordapp.com/api/guilds/683766194884575355/widget.json');
        $decoded = json_decode($request, true)['members'];

        return array_slice($decoded, 0, 10);
    }

    /**
     * Return index view
     * @return Factory|View
     */
    public function index() {

        return view('index')->with('players', $this->getServerPlayers())->with('clients', $this->getServerClients())->with('members', $this->getDiscordMembers());
    }
}

这是我的看法

<div class="row">
                <div class="col-lg-12">
                    <div class="tab-content" id="pills-tabContent">
                        <div class="tab-pane fade show active" id="pills-all-bets" role="tabpanel"
                            aria-labelledby="pills-all-bets-tab">
                            <div class="responsive-table">
                                <table class="table">
                                    <thead>
                                        <tr>
                                            <th scope="col">Nom du joueur</th>
                                            <th scope="col">Statut</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($players as $player)
                                        <tr>
                                            <td><img src="{{ $player->avatar_url }}">{{ $player->username }}</td>
                                            <td>En ligne</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

我认为问题有点愚蠢,但我对此感到受阻,我需要一些帮助!

谢谢。

评论
  • paut
    paut 回复

    You should use avatar_url like this:

    <td><img src="{{ $player['avatar_url'] }}">{{ $player['username'] }}</td>
    

    因为传递给视图的是数组,而不是对象。

  • 云想衣裳
    云想衣裳 回复

    The problem is that json_decode returns an array and not an object.

    您可以使用以下方法将参数传递给视图:

            return view('index',[
                'players' => $this->getServerPlayers(),
                'clients' => $this->getServerClients(),
                'members' => $this->getDiscordMembers(),
            ]);
    

    然后使用以下命令在视图中显示它们:

    <img src="{{ $player['avatar_url'] }}">{{ $player['username'] }}