I'm trying to select Games with Screenshot relationship, but only where at least one Screenshot image exists. For some reason it also includes Games without existing screenshots and I am really unsure why that happens.
End-result expected:
Get 3 games, with one screenshot in size "t_screenshot_huge" where hypes are greater than 1, order results by random so always get different results.
This is my query:
Game::with([
'screenshots' => function ($query) {
$query->where('size_label', '=', 't_screenshot_huge');
}
])->where('hypes', '>', 1)->orderByRaw('RAND()')->take(3)->get();
That's the resulting Collection (first Game, without screenshot images)
Collection {#342 ▼
#items: array:3 [▼
0 => Game {#353 ▼
#guarded: array:1 [▶]
#connection: "mysql2"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [▼
"id" => 22
"slug" => "backgammon-masters"
"name" => "Backgammon Masters"
"summary" => null
"storyline" => null
"hypes" => 7
"popularity" => 0.0
"rating" => 20.0
"rating_count" => 0
"aggregated_rating" => 0.0
"aggregated_rating_count" => 0
"total_rating" => 20.0
"total_rating_count" => 0
"first_release_date" => null
"status" => "Released"
"esrb" => "RP (Rating Pending)"
"pegi" => "16"
"website" => null
"deleted_at" => null
"created_at" => "2018-03-06 14:16:17"
"updated_at" => "2018-03-06 14:16:17"
"cover_image" => null
"cover_image_original" => null
]
#original: array:23 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"screenshots" => Collection {#354 ▼
#items: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#forceDeleting: false
}
1 => Game {#355 ▶}
2 => Game {#362 ▶}
]
}