Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Stillfinder's avatar

where in related models

Hi,

I have PickEmContest model with:

public function rounds()
{
    return $this->belongsToMany(PickEmRound::class);
}

and I'm trying:

$contest = PickEmContest::first();
$round = $contest->rounds->where('visible', true);
dd($round);

and see the next: Collection {#431 ▼ #items: [] }

visible - it's field in PickEmRound model

When I'm trying $round = $contest->rounds; dd($round);

I see:

Collection {#434 ▼
  #items: array:2 [▼
    0 => PickEmRound {#449 ▶}
    1 => PickEmRound {#445 ▼
      #fillable: array:3 [▶]
      #casts: array:2 [▶]
      #hidden: array:2 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:6 [▼
        "id" => 2
        "name" => "Round 2"
        "created_at" => "2018-07-27 10:17:50"
        "updated_at" => "2018-08-03 12:35:49"
        "visible" => 1
        "active" => 0
      ]
      #original: array:8 [▶]
      #dates: []
      #dateFormat: null
      #appends: []
      #events: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
    }
  ]
}    

So, one of PickEmRound has "visible" => 1

What am I doing wrong?

Thank you for any help.

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

Try using the Query Builder.

$contest = PickEmContest::first();

$round = $contest->rounds()->where('visible', true)->get(); // or first()

Please or to participate in this conversation.