Laravel 4.2 Eloquent Check ID

时间:2015-02-27 07:55:59

标签: php laravel eloquent

我有两张桌子

" hostgame" - >举办游戏的地方 "游戏" - >托管的每个游戏都在这里获取他们各自的信息

我们得到了#34; game_id"在" hostgame"表,截至目前,所有游戏都发布在网站上,我想根据" game_id"过滤那些游戏。在" hostgame"表,只显示已托管的游戏。

我该怎么做?

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

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

1 个答案:

答案 0 :(得分:0)

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

  1. 将单个游戏的功能添加到GameController.php public function getSingleGame($id) { // logic here }
  2. 接下来,将以下(Controller)路由添加到routes.php以在Route,Controller和模板之间建立链接: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,现在由路由设置。
  4. 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 - &gt; http://daylerees.com/codebright/controllers和版本5观看了这个精彩的系列:https://laracasts.com/series/laravel-5-fundamentals