约束Eager Loads明显不起作用

时间:2016-08-22 01:10:04

标签: laravel-5.2

import Foundation



class Hero {

    var id: String?
    var attributeid: String?
    var name: String?
    var title: String?
    var description: String?
    var role: String?
    var type: String?
    var gender: String?
    var franchise: String?
    var difficulty: String?
    var icon: String?
    var ratings: Ratings?
    var stats: Stats?
    var talents: [Talent]?
    
    
  
    
    
    init(id: String, attributeid: String, name: String, title: String, description: String, role: String, type: String, gender: String, franchise: String, difficulty: String, icon: String){
        self.id = id
        self.attributeid = attributeid
        self.name = name
        self.title = title
        self.description = description
        self.role = role
        self.type = type
        self.gender = gender
        self.franchise = franchise
        self.difficulty = difficulty
        self.icon = icon

        
    }
    
}


class Ratings {
    var damage: Int?
    var utility: Int?
    var survivability: Int?
    var complexity: Int?
    
    
    init(damage: Int, utility: Int, survivability: Int, complexity: Int) {
        self.damage = damage
        self.utility = utility
        self.survivability = survivability
        self.complexity = complexity
    }
}

class Stats {
    var hp: Int?
    var hpPerLevel: Int?
    var hpRegen: Double?
    var hpRegenPerLevel: Double?
    var mana: Int?
    var manaPerLevel: Int?
    var manaRegen: Double?
    var manaRegenPerLevel: Double?
   
    
    init(hp: Int, hpPerLevel: Int, hpRegen: Double, hpRegenPerLevel: Double, mana: Int, manaPerLevel: Int, manaRegen: Double, manaRegenPerLevel: Double) {
        self.hp = hp
        self.hpPerLevel = hpPerLevel
        self.hpRegen = hpRegen
        self.hpRegenPerLevel = hpRegenPerLevel
        self.mana = mana
        self.manaPerLevel = manaPerLevel
        self.manaRegen = manaRegen
        self.manaRegenPerLevel = manaRegenPerLevel
    }
    
    
}

class Talent {
    var id: String?
    var name: String?
    var description: String?
    var cooldown: Double?
    var prerequisite: String?
    var icon: String?
   

    
    init(id: String, name: String, description: String, cooldown: Double, prerequisite: String, icon: String) {
        self.id = id
        self.name = name
        self.description = description
        self.cooldown = cooldown
        self.prerequisite = prerequisite
        self.icon = icon
        
    }
    
    
}

class Ability {
    var id: String?
    var name: String?
    var description: String?
    var shortcut: String?
    var cooldown: Double?
    var manaCost: Double?
    var manaCostPerSecond: Double?
    var aimType: String?
    var heroic: Bool?
    var trait: Bool?
    var mount: Bool?
    var icon: String?
    
    init(id: String, name: String, description: String, shortcut: String, cooldown: Double, manaCost: Double, manaCostPerSecond: Double, aimType: String, heroic: Bool, trait: Bool, mount: Bool, icon: String){
        self.id = id
        self.name = name
        self.description = description
        self.shortcut = shortcut
        self.cooldown = cooldown
        self.manaCost = manaCost
        self.manaCostPerSecond = manaCostPerSecond
        self.aimType = aimType
        self.heroic = heroic
        self.trait = trait
        self.mount = mount
        self.icon = icon

    }
    
}

func parseData(){
    let urlString = "http://heroesjson.com/heroes.json"
    let session = NSURLSession.sharedSession()
    let url = NSURL(string: urlString)!
    
    session.dataTaskWithURL(url) { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in
        
        if let responseData = data {
            
            do {
                let json = try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments)
                
                if let dict = json as? [Dictionary<String, AnyObject>] {

                    for x in 0 ..< dict.count {
                        
                        
                        if let id = dict[x]["id"], let attributeid = dict[x]["attributeid"], let name = dict[x]["name"], let title = dict[x]["title"], let description = dict[x]["description"], let role = dict[x]["role"], let type = dict[x]["type"], let gender = dict[x]["gender"], let franchise = dict[x]["franchise"], let difficulty = dict[x]["difficulty"], let icon = dict[x]["icon"] {
                            
                            let hero = Hero(id: id as! String, attributeid: attributeid as! String, name: name as! String, title: title as! String, description: description as! String, role: role as! String, type: type as! String, gender: gender as! String, franchise: franchise as! String, difficulty: difficulty as! String, icon: icon as! String)
                            
                             // Parse Ratings Class
                            
                            if let dataArray = dict[x]["ratings"] as? Dictionary<String, Int> {
                                
                                if let damage = dataArray["damage"], let utility = dataArray["utility"], let survivability = dataArray["damage"], let complexity = dataArray["complexity"] {
                                    
                                    let rating = Ratings(damage: damage , utility: utility , survivability: survivability , complexity: complexity )
                                    hero.ratings = rating
                                    //print("\(hero.name) has a damage rating of \(hero.ratings!.damage)")
                                    
                                }
                            }
                            
                            //Parse Stats Class
                            if let statsArray = dict[x]["stats"] as? Dictionary<String, AnyObject> {
                                

                                if let dummy = statsArray[hero.id!]{//error handleing for vikings
                                    
                                    
                                    if let hp = statsArray[hero.id!]!["hp"], let hpPerLevel = statsArray[hero.id!]!["hpPerLevel"], let hpRegen = statsArray[hero.id!]!["hpRegen"], let hpRegenPerLevel = statsArray[hero.id!]!["hpRegenPerLevel"], let mana = statsArray[hero.id!]!["mana"], let manaPerLevel = statsArray[hero.id!]!["manaPerLevel"], let manaRegen = statsArray[hero.id!]!["manaRegen"], let manaRegenPerLevel = statsArray[hero.id!]!["manaRegenPerLevel"] {
                                        
                                        
                                        let stats = Stats(hp: hp as! Int, hpPerLevel: hpPerLevel as! Int, hpRegen: hpRegen as! Double, hpRegenPerLevel: hpRegenPerLevel as! Double, mana: mana as! Int, manaPerLevel: manaPerLevel as! Int, manaRegen: manaRegen as! Double, manaRegenPerLevel: manaRegenPerLevel as! Double)
                                        
                                        
                                        hero.stats = stats
                                    }
                                    
                                }//closes let dummy
                                
                                
                            }
                            
                            //Parse Talent Class
                            if let talentsArray = dict[x]["talents"] as? Dictionary<String, AnyObject> {
                                
                                for y in 0 ..< dict.count {
                                    if let allHeroTalents = talentsArray["\(y)"]{
                                        for z in 0 ..< allHeroTalents.count {

                                            
                                                let id = allHeroTalents[z]["id"]
                                                let name = allHeroTalents[z]["name"]
                                                let description = allHeroTalents[z]["description"]
                                                let cooldown = allHeroTalents[z]["cooldown"] ?? 0.0
                                                let prerequisite = allHeroTalents[z]["prerequisite"] ?? ""
                                                let icon = allHeroTalents[z]["icon"]
                                            
                                            
                                                let sId = id as? String
                                                let sName = name as? String
                                                let sDescription = description as? String
                                                //let iCooldown = cooldown as! Double
                                                //let sPrerequisite = prerequisite as! String
                                                let sIcon = icon as? String
                                                //let talent = Talent(id: sId!, name: sName!, description: sDescription!, cooldown: iCooldown, prerequisite: sPrerequisite, icon: sIcon!)
                                            print("\(hero.name) has talent \(cooldown)")
                                            
                                            
                                        }
                                    }
                                }
                                
                            }
                            
                            // Parse Ability Class

                            if let abilitiesArray = dict[x]["abilities"] as? Dictionary<String, AnyObject> {
                                for c in 0 ..< abilitiesArray.count {
                                   
                                    for d in 0 ..< abilitiesArray.count {
                                        
                                    if let dummy = abilitiesArray[hero.id!]{
                                        
                                            
                                            let id = abilitiesArray[hero.id!]!["id"]
                                            let name = abilitiesArray[hero.id!]!["name"]
                                            let description = abilitiesArray[hero.id!]!["description"]
                                            let shortcut = abilitiesArray[hero.id!]!["shortcut"]
                                            let cooldown = abilitiesArray[hero.id!]!["cooldown"]
                                            let manaCost = abilitiesArray[hero.id!]!["manaCost"]
                                            let manaCostPerSecond = abilitiesArray[hero.id!]!["manaCostPerSecond"]
                                            let aimType = abilitiesArray[hero.id!]!["aimType"]
                                            let heroic = abilitiesArray[hero.id!]!["heroic"]
                                            let trait = abilitiesArray[hero.id!]!["trait"]
                                            let mount = abilitiesArray[hero.id!]!["mount"]
                                            let icon = abilitiesArray[hero.id!]!["icon"]

                                            
                                            let sId = id as? String
                                            let sName = name as? String
                                            let sDescription = description as? String
                                            let sShortcut = shortcut as? String
                                            let sCooldown = cooldown as? Double
                                            let sManaCost = manaCost as? Double
                                            let sManaCostPerSecond = manaCostPerSecond as? Double
                                            let sAimType = aimType as? String
                                            let sHeroic = heroic as? Bool
                                            let sTrait = trait as? Bool
                                            let sMount = mount as? Bool
                                            let sIcon = icon as? String
                                            
                                            
//                                            let abilities = Ability(id: sId!, name: sName!, description: sDescription!, shortcut: sShortcut!, cooldown: sCooldown!, manaCost: sManaCost!, manaCostPerSecond: sManaCostPerSecond!, aimType: sAimType!, heroic: sHeroic!, trait: sTrait!, mount: sMount!, icon: sIcon!)
                                        
                                        
                                        
                                    }
                                }
                                
                                }
                                
                                
                                
                                
                            }
                            
                            
                            heroes.append(hero)
                        }
                    }
                }
            } catch {
                print("Could not serialize")
            }
        }
        
        
        }.resume()
    
    
    
    
}

我在这里尝试的只是获取不同的值,但结果没有(我所有部门的空数组)。这是为什么?

2 个答案:

答案 0 :(得分:0)

您可以尝试在没有DB::raw()函数的->distinct()语句的情况下执行此操作。

Department::with(['dispatches' => function ($query) {
            $query->distinct('dispatchedItem'));
        }])->get();

答案 1 :(得分:0)

我再一次回答了我的问题。我只需要在折扣后加上括号。这是因为我在一个集合上调用了一个查询构建器方法而它没有工作,所以当你放置括号时,你会得到一个查询构建器实例来使用你的查询构建器方法。希望它可以帮到某人。