Laravel 4.2 Eloquent Check ID


Laravel 4.2 Eloquent Check ID

我得到了两张表

"hostgame"->游戏所在地"games"->每个托管的游戏都会在这里获得各自的信息

我们会在"hostgame"表中得到"game_id",截至目前,所有游戏都发布在网站上,我想根据"hostgames"表中的"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

谢谢,现在所有的比赛都在按各自的日期进行。我只想展示已经举办过的比赛。

有多种方法可以实现您的目标。既然你已经在主机列表中有了你的游戏id,试试这个:

  1. 在GameController.php public function getSingleGame($id) { // logic here }中添加单个游戏的函数
  2. 接下来,将以下(控制器)路由添加到routes.php中,以在路由、控制器和模板之间建立链接:Route::get('game/draft/{id}', ['as' => 'show_single_game', 'uses' => 'GameController@getSingleGame']);您现在可以通过特定名称访问该路由(show_single_game)
  3. 现在你可以这样写你的URL路由:{{ link_to_route('show_single_game', $game->home_name . " Vs " . $game->away_name, array($game->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);
}

我建议你读这本书http://daylerees.com/codebright/controllers对于版本5,请观看这一精彩系列:https://laracasts.com/series/laravel-5-fundamentals