【问题标题】:Laravel 4.2 Eloquent Check IDLaravel 4.2 Eloquent 检查 ID
【发布时间】:2015-02-27 07:55:58
【问题描述】:

我有两张桌子

"hostgame" -> 托管游戏的地方 “游戏”-> 托管的每个游戏都在这里获取各自的所有信息

我们将在“hostgame”表中获得“game_id”,截至目前,所有游戏都发布在网站上,我想在“hostgame”表中根据“game_id”过滤这些游戏,并且仅显示已托管的游戏。

我该怎么做?

GameController.php

    public function select()
{
    // TODO: Pls revise this. This is not the right way to do
    $weekly_dates = $this->get_days( Config::get('game.year'), 9  );

    $this->params['weekly_dates'] = $weekly_dates;
    $this->params['weekly_games'] = array();

    foreach($weekly_dates as $date ) {

        $from = date('Y-m-d 00:00:00', strtotime($date));
        $to = date('Y-m-d 23:59:59', strtotime($date));

        //foreach($hostgames as $hostgame){
        $this->params['hostgame'] = Hostgame::all();
        dd($this->params['hostgame']);
        $this->params['weekly_games'][$date] = Game::whereRaw('schedule >= ? and schedule <= ?', array($from, $to) )->get();
        //}

        //$this->params['weekly_games'][$date] = Game::has()('schedule >= ? and schedule <= ?', array($from, $to) )->hostgames();
        //$this->params['weekly_games'][$date] = Hostgame::whereHas('game', function($q) {
            //$q->whereRaw('schedule >= ? and schedule <= ?', array('34365', '2435') )->get();
        //});

    }

select.blade.php

          <?php $day = 0 ?>
      @foreach ($weekly_games as $date => $games )
      @if($games)
      <div id="slide-content-{{ $day }}">
        <h2>Sunday Games {{ $date }}</h2>
          @foreach ($games as $game )

          <p><a href="{{ URL::to('game/draft/'.$game->id) }}"> {{ $game->home_name }} Vs {{ $game->away_name }}</a></p>
          @endforeach
        @endif
      </div>
      <?php $day++ ?>
      @endforeach

谢谢,现在所有游戏都按各自的日期显示。 我只想显示已托管的游戏。

【问题讨论】:

  • 你用的是eloquent模型吗? -> laravel.com/docs/master/eloquent
  • 是的,我做到了.. 但我在问是否有人知道我与此有什么关系,我非常绝望地去询问。但如果它能够用雄辩的方式做到这一点,那就是我问的原因。
  • 好的,如果我理解正确 - 您有两张桌子,一张用于托管的游戏,一张用于游戏本身,对吗?那么,每个主机都属于一个游戏?
  • 是的,它确实属于他们自己的游戏,所以“hostgame”表是创建游戏的地方,而“games”表是获取信息的地方,所以具体来说,我试图找到“hostgame.game_id”->“game.id”,如果 id 相同,那么它将提取“schedule”数据,以及“home_team”和“away_team”并显示前端的游戏信息
  • 好的,我明白了。因此,请将您的 Game.php 和 Hostgame.php 模型添加到 gist (gist.github.com) 或您的帖子中,我将帮助您建立正确的关系

标签: php laravel eloquent


【解决方案1】:

有多种方法可以实现您的目标。由于您的主机列表中已经有您的游戏 ID,请尝试以下操作:

  1. 将单个游戏的功能添加到 GameController.php public function getSingleGame($id) { // logic here }
  2. 接下来,将以下(控制器)路由添加到您的 routes.php 以在路由、控制器和模板之间建立链接:Route::get('game/draft/{id}', ['as' =&gt; 'show_single_game', 'uses' =&gt; 'GameController@getSingleGame']); 您现在可以通过特定名称访问路由(show_single_game)。
  3. 现在你可以像这样编写你的 URL 路由:{{ link_to_route('show_single_game', $game-&gt;home_name . " Vs " . $game-&gt;away_name, array($game-&gt;id) ) }} Laravel 会为你创建链接,所以不用担心 URL,它现在由路由设置。

GameController getSingleGame 的逻辑:

public function getSingleGame($id) {
    // find the game by id
    $game = Game::findOrFail($id); 

    // assuming your template is located in views/games/show.blade.php
    return View::make('games/show')
        ->with('game', $game);
}

我建议阅读本书的 laravel 4.X -> http://daylerees.com/codebright/controllers 和版本 5 观看这个很棒的系列:https://laracasts.com/series/laravel-5-fundamentals

【讨论】:

  • 非常感谢您尽可能清楚地解释......非常感谢您
  • 我没有测试过这个,但它应该可以工作(希望如此;)),所以只要问,我会编辑帖子!
猜你喜欢
  • 2014-12-10
  • 1970-01-01
  • 2023-04-03
  • 2016-09-20
  • 2015-01-27
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
相关资源
最近更新 更多