Bir veritabanında bir tabloda kayıtlı tüm oyunların listesini alan, bulunan her oyunda yineleyen ve başka bir veritabanındaki verileri getiren ve son olarak benim Oyun modelimde bir yöntem var ve son olarak bir veritabanından rastgele bir görüntü URL'si yakalar ve istatistik nesnesine ekler. Şaşkın mı?Laravel :: İki DB sorgu nesnesini birleştirin
burada yöntemidir, artık karıştırılmamalı: nesne kullanılamaz:
public static function getStats($player, $cache = 30) {
// Return a collection of all games registered from the website database
$games = Game::get(['game']);
foreach($games as $game) {
// For each iteration, return the statistics from the games database for that game
// Grab game meta information from the website database, $game->game is the game name.
$list = Game::where('game', $game->game)->remember($cache)->first();
// From the game database, retrieve the statistics
$stats[$game->game] = DB::connection('game')->table($list->stats_table)
->select(DB::raw($list->statistics))
->where('Username', $player)
->remember($cache)
->first();
// Grab a random map image from the game database
$stats[$game->game]['map'] = DB::connection('game')->table('Maps')
->select('Image')
->orderBy(DB::raw('RAND()'))
->remember($cache)
->first();
}
// Finally, return the statistics from the game paired with a random map image from the game
return $stats;
}
Ben tek bir yerde yani $ üzerine istatistikleri harita ekleme sorgusu istiyorum, şu anda bu yöntem nedeniyle başarısız stdClass'i dizi olarak yazın
Haritalar olmadan, $ stats üzerinde döngü yaparken, oyunun adı anahtar olan bir oyun istatistikleri dizisidir.
Hala kafanız karıştıysa (sizi suçlamıyorum), sonra bir yorum bırakın ve daha fazlasını açıklayacağım.
DÜZENLEME: laravel 4.2 çalıştıran
public static function getStats($player, $cache = 30) {
// Return a collection of all games registered from the website database
$games = Game::get(['game']);
foreach($games as $game) {
// For each iteration, return the statistics from the games database for that game
// Grab game meta information from the website database
$list = Game::where('game', $game->game)->remember($cache)->first();
// From the game database, retrieve the statistics
$stats[$game->game] = DB::connection('game')->table($list->stats_table)
->select(DB::raw($list->statistics))
->where('Username', $player)
->remember($cache)
->first();
// Grab a random map image from the game database
$map = DB::connection('game')->table('Maps')
->select('Image')
->orderBy(DB::raw('RAND()'))
->remember($cache)
->first();
$stats[$game->game] = $stats[$game->game]->merge($map);
}
// Finally, return the statistics from the game paired with a random map image from the game
return $stats;
}
tanımsız yöntem stdClass için Çağrısı :: birleştirme (sonuçlanır):
İşte ->merge()
kullanarak benim denemesi.
Yani, sonuç olacağı '$ veri [$ oyun-> Oyun] -> birleştirme ($ harita)' 'ile $ map' $ datadan yeniden adlandırılıyor [$ game-> game] ['map'] – davidxd33
Deneme amaçlı düzenlemeye bakın. – davidxd33
Ne almaya çalıştığınızı anlayamıyorum. Bu birleştirme() sadece toplanan koleksiyonlar için geçerlidir. – thegeekajay