How to access model hasMany Relation with where condition?

前端 未结 8 1383
迷失自我
迷失自我 2020-12-08 00:02

I created a model Game using a condition / constraint for a relation as follows:

class Game extends Eloquent {
             


        
8条回答
  •  余生分开走
    2020-12-08 00:52

    I think that this is the correct way:

    class Game extends Eloquent {
        // many more stuff here
    
        // relation without any constraints ...works fine 
        public function videos() {
            return $this->hasMany('Video');
        }
    
        // results in a "problem", se examples below
        public function available_videos() {
            return $this->videos()->where('available','=', 1);
        }
    }
    

    And then you'll have to

    $game = Game::find(1);
    var_dump( $game->available_videos()->get() );
    

提交回复
热议问题