Laravel 4公会网站

时间:2014-01-20 22:14:19

标签: php laravel laravel-4

我正在创建一个游戏网站。有行会,我想加载所有成员。问题是,在列表中,它只显示领导者,而不是其他人。这是代码:

  public function showRequest($name = false)
  {
    if (!$name) return Redirect::to('/');

    $name = urldecode($name);

    $load = Guild::whereName($name)->first();

    if (!$load) return Redirect::to('/');

    $owner_id = $load->ownerid;
    $real = Player::whereId($owner_id)->first();

    $members = Guildsmembers::whereGuildId($load->id)->orderBy('rank_id')->get();
    $count = 0;
    $level = 0;
    $high = 0;
    $low = 0;
    $avg = 0;

    foreach ($members as $tt) {
      $count = $count + Playersonline::wherePlayerId($tt->player_id)->count();
      $player = Player::whereId($tt->player_id)->first();
      $ranks = Guildsranks::whereId($tt->rank_id)->whereGuildId($load->id)->first();
      $cool[$player->name] = array('name' => $player->name, 'level' => $player->level, 'voc' => $player->vocation, 'nickname' => $tt['nick'], 'rank' => $ranks['name']);      

      $level = $level + $cool[$player->name]['level'];

      if ($player->level > $high) 
      {
        $high = $player->level;
        $top_name = $player->name;
      }

      $low = $player->level;
      $low_name = $player->name;

      if ($player->level < $low) 
      {
        $low = $player->level;
        $low_name = $player->name;
      }

      $invited = Guildsinvites::whereGuildId($load->id)->get();
      $invitation = false;

      if ($invited)
      {
        foreach ($invited as $yy)
        {
          $tu = Player::whereId($yy->player_id)->first();
          $join = User::whereId($tu->account_id)->first();

          if (Auth::check())
          {
            if ($join->id == Auth::user()->id)
            {
              $invitation[$yy->player_id] = array('id' => $tu->id, 'name' => $tu->name, 'join' => true);
            } else {
              $invitation[$yy->player_id] = array('id' => $tu->id, 'name' => $tu->name, 'join' => false);
            }
          }
          else
          {
            $invitation[$yy->player_id] = array('id' => $tu->id, 'name' => $tu->name, 'join' => false);
          }
        }
      }
      $stats = array('owner' => $real->account_id, 'low_name' => $low_name, 'low' => $low, 'count' => $count, 'level' => $level, 'top' => $high, 'top_name' => $top_name, 'avg' => $avg);

      return View::make('main.guildview', array('invited' => $invitation, 'msg' => Session::get('errors'), 'stats' => $stats, 'load' => $cool, 'info' => $load));
    }
}

在公会列表网站上,它说有两个成员,但它只加载其中一个成员。如果您还有其他需要,请发表评论。感谢。

1 个答案:

答案 0 :(得分:2)

如果我正确阅读了你的代码,你就有了一个公会ID和这个

$members = Guildsmembers::whereGuildId($load->id)->orderBy('rank_id')->get();

确实应该加载那些公会ID与$load->id相同的成员。

$load是一个公会:

$load = Guild::whereName($name)->first();

所以我相信你的代码应该才能工作。我不会打电话给Guildsmembers::all()因为那会加载所有公会成员......好吧,所有公会(BTW:all()都会返回一个收藏,所以你有使用sortBy而非orderBy)。

TEST 1

验证whereGuildId()是否返回两个成员。如果没有,请检查公会ID是否正确,并确保whereGuildId()的范围功能完成了它应该做的事情(更好的是,发布该部分代码)。

如果可以,那么

TEST 2

验证您的视图确实在其检索的所有成员之间循环并输出所有成员。

附录

我已经重新缩进了你的代码,除非我弄错了,看起来好像你从循环内部返回你的函数。因此循环只执行一次,并且只返回$members的第一个元素。

如果这个确实是问题,你首先需要在循环之外的对象中积累你想要的任何信息:

$memberInfo = array();
foreach ($members as $member) {
...
}

然后你必须将这个对象传递给视图:

$memberInfo = array();
foreach ($members as $member) {
...
}
...

return View::make(
    'main.guildview',
    array('invited' => $invitation, 'msg' => Session::get('errors'), 'stats' => $stats, 'load' => $cool, 'info' => $load, 'members' => $memberInfo));

不幸的是,只有你知道才能放入$memberInfo。在“查看”中,您将迭代$memberInfo以显示其内容。